简体   繁体   English

htaccess重写规则

[英]htaccess Rewrite Rule

I use htaccess to rewrite my URL's so here is what I have: 我使用htaccess重写我的URL,所以这里是我拥有的:

RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L]

So if I go to domain.com/services then it works perfect, but what I want to do is make it so if they do type in domain.com/services.php then it will work instead of not being found. 因此,如果我转到domain.com/services,那么它可以完美地工作,但是我要做的就是做到这一点,因此,如果他们输入domain.com/services.php,它将可以正常工作,而不是找不到。

Also.. if I go to domain.com/services/ (with a trailing slash) then it acts like it's not found. 另外..如果我转到domain.com/services/(带有斜杠),那么它的行为就像找不到它。 Why is this? 为什么是这样?

You have limited "rewrite loop" (limited to 2 iterations only). 您具有有限的“重写循环”(仅限于2次迭代)。 Use these rules: 使用以下规则:

# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]

RewriteRule ^([a-z0-9_\-]+)(\.php)?$ index.php?page=$1 [NC,L,QSA]

If you do not like it, here is an alternative (but I still prefer #1, but maybe this one will be better for your setup/app logic -- this assumes that index.php is located in website root folder): 如果您不喜欢它,则可以选择另一种方法(但是我仍然更喜欢#1,但是对于您的设置/应用逻辑来说,这可能会更好-假设index.php位于网站根文件夹中):

RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^([a-z0-9_\-]+)(\.php)?$ index.php?page=$1 [NC,L,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)\.php$ index.php?page=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)\/$ index.php?page=$1 [L]

add slash as aceptable possible character (note ^([a-zA-Z0-9_-]+) \\/ $) 将斜杠添加为可接受的字符(请注意^([a-zA-Z0-9 _-] +) \\/ $)

For the second requirement (the php extension) try this 对于第二个要求(php扩展名),请尝试此操作

RewriteRule ^([a-zA-Z0-9_-]+)(\.php)?$ index.php?page=$1 [L]

untested, but it should work. 未经测试,但应该可以。

This should work for you: 这应该适合你:

RewriteRule ^([a-zA-Z0-9_-]+) index.php?page=$1 [L] RewriteRule ^([a-zA-Z0-9 _-] +)index.php?page = $ 1 [L]

You aren't allowing a period, so the '.php' fails the expression since it is terminated with $ 您不允许使用句号,因此'.php'使表达式失败,因为它以$终止

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM