Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
559 views
in Technique[技术] by (71.8m points)

apache - .htaccess重写规则自动更改url(.htaccess rewrite rule change url automatic)

I'm new to htaccess and I need some help how do I change the url automatically from from .htaccses if I write url like:

(我是htaccess的新手,我需要一些帮助,如果我这样写url,该如何从.htaccses自动更改url:)

url/index.php/pages

url will change automatic to:

(网址将自动更改为:)

url/pages

Thanks in advance,

(提前致谢,)

Andhi

(安地)

  ask by Andhika Bhakti translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This appears to be pretty straight forward, you would have found hundreds of existing answers and examples alone here on StackOberflow...

(这似乎很简单,您可以在StackOberflow上单独找到数百个现有答案和示例。)

RewriteEngine on
RewriteRule ^/?index.php/pages/?$ /pages [R=301]

This assumes that in your question, in the given path url/index.php/pages the "url" refers to a prefix of protocol scheme and host name, so would usually be written as https://example.com/index.php/pages ...

(假设在您的问题中,在给定路径url/index.php/pages ,“ url”是指协议方案和主机名的前缀,因此通常写为https://example.com/index.php/pages ...)

It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up.

(一个好主意是从302临时重定向开始,然后在确定一切都已正确设置之后,才将其更改为301永久重定向。)

That prevents caching issues while trying things out...

(这样可以防止在尝试时缓存问题...)

I dare say however that you also need the corresponding internal rewrite to again be able to process such redirected requests.

(不过我敢说,您还需要进行相应的内部重写,才能再次处理此类重定向请求。)

Adding that the example looks like this:

(添加示例如下所示:)

RewriteEngine on
RewriteRule ^/?index.php/pages/?$ /pages [R=301]
RewriteRule ^/?pages/?$ /index.php/pages [END]

In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server.

(如果您使用上述规则收到内部服务器错误(http状态500),则很可能是您运行了非常旧版本的apache http服务器。)

You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case.

(在这种情况下,您将在http服务器错误日志文件中看到对不支持的[END]标志的明确提示。)

You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.

(您可以尝试升级或使用旧的[L]标志,在这种情况下它可能会起作用,尽管这在一定程度上取决于您的设置。)

This get more complex if your question does not only target the single, specific path /index.php/pages but actually any "pages" to follow in the path after the leading /index.php/ .

(如果您的问题不仅针对单个特定路径/index.php/pages而且实际上针对在/index.php/之后的路径中要遵循的任何“页面”,则情况将变得更加复杂。)

For that you'd need something a bit more complex:

(为此,您需要一些更复杂的东西:)

RewriteEngine on
RewriteRule ^/?index.php/(.*)$ /$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php/
RewriteRule ^/?(.*)$ /index.php/$1 [END]

This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file).

(此实现将在http服务器主机配置或动态配置文件(“ .htaccess”文件)中同样起作用。)

Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host.

(显然,重写模块需要加载到http服务器内部并在http主机中启用。)

In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.

(如果使用动态配置文件,则需要注意在主机配置中完全启用了它的解释,并且该解释位于主机的DOCUMENT_ROOT文件夹中。)

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess").

(还有一个一般性说明:您应该始终喜欢将此类规则放在http服务器主机配置中,而不要使用动态配置文件(“ .htaccess”)。)

Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server.

(这些动态配置文件增加了复杂性,通常是导致意外行为,难以调试的原因,并且确实降低了http服务器的速度。)

They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

(仅当您无法访问真正的http服务器主机配置(阅读:真正便宜的服务提供商)或坚持编写自己的规则的应用程序(这显然是安全的噩梦)时,才提供它们作为最后的选择。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...