开发者

.htaccess Not Following Rule

I have the following .htaccess file.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^awesomeslash/ /somepage.php [NC,PT]    
RewriteRule ^([^/\.]*)/?([^/\.]*)/?([^/\.]*)/?$ /template2.php?slash1=$1&slash2=$2&slash3=$3 [L]

I've running into a problem with the rule:

 RewriteRule ^awesomeslash/ /somepage.php [NC,PT]  

When I go to http://www.mysite.com/awesomeslash/

It i开发者_Python百科s not loading somepage.php instead it is following the run under it and going to template2.php


Firstly: This does not make much sense if you want rewrite /awesomeslash/ to /somepage.php:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^awesomeslash/ /somepage.php [NC,PT]    

RewriteRule ^([^/\.]*)/?([^/\.]*)/?([^/\.]*)/?$ /template2.php?slash1=$1&slash2=$2&slash3=$3 [L]

I assume you wanted this instead:

RewriteRule ^awesomeslash/ /somepage.php [NC,PT]    

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^([^/\.]*)/?([^/\.]*)/?([^/\.]*)/?$ /template2.php?slash1=$1&slash2=$2&slash3=$3 [L]

Secondly:

  • You need L flag next to [NC,PT]: [NC,PT,L] -- this will tell Apache to not to process other rules.

  • You also need to add $ at the end of match pattern to make the rule match only this URL and not /awesomeslash/something-else.

    RewriteRule ^awesomeslash/$ /somepage.php [NC,PT,L]


I think adding L to [NC,PT] should fix it.

"The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, no further rules will be processed. This corresponds to the last command in Perl, or the break command in C. Use this flag to indicate that the current rule should be applied immediately without considering further rules." http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l


You have to put a $ at the end of match condition and add the L flag like that:

RewriteRule ^awesomeslash/$ /somepage.php [L,NC,PT]  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜