开发者

.htaccess exception for several subfolders

开发者 https://www.devze.com 2023-01-18 05:03 出处:网络
I am having some troubles with my script.. when I access http://www.domain.com/this-is-a-topic I want it to redirect to http://www.domain.com/index.php?t=this-is-a-topic... BUT when it says \"adminis

I am having some troubles with my script..

when I access http://www.domain.com/this-is-a-topic I want it to redirect to http://www.domain.com/index.php?t=this-is-a-topic... BUT when it says "administration", "sitemap" or some other things, I want it to look either another rewrite condition or simply just get http://www.domain.com/administration/

Here is my code:

RewriteCond %{REQUEST_URI} !(administration|sitemap)/(.*)\. [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d


# Dync links

RewriteRule ^([a-zA-Z0-9-]+)/lang:([0-9]+)?$    index.php?t=$1&l=$2 [L]
RewriteRule ^lang:([0-9]+)?$                    index.php?l=$1 [L]


RewriteRule ^sitemap/?$                         index.php?sitemap=sitemap [L]

RewriteRule ^error/([0-9]+)?$                   index.php?error=$1
开发者_开发知识库RewriteRule ^([a-zA-Z0-9-]+)/?$             index.php?t=$1 

The problem is, that when I write /administration/ it goes to http://www.domain.com/index.php?t=/administration/ instead of going to http://www.domain.com/administration/index.php


There's two problems there. First, your condition

RewriteCond %{REQUEST_URI} !(administration|sitemap)/(.*)\. [NC]

will always be true, because the value of %{REQUEST_URI} always has a leading forward slash (so the pattern will always not match). You also likely don't want to look for that dot for the cases where you browse to the sitemap and administration folders directly.

Secondly, the conditions only apply to the very next rule, so while the folders would be ignored for the rule

RewriteRule ^([a-zA-Z0-9-]+)/lang:([0-9]+)?$    index.php?t=$1&l=$2 [L]

they would not be ignored for

RewriteRule ^([a-zA-Z0-9-]+)/?$                 index.php?t=$1 

There's several ways to fix this, but the easiest might be to just ignore the rest of the rules in the case where any of your conditions match upfront, like this:

# Don't rewrite and stop processing if any of the following is true
RewriteCond %{REQUEST_URI} ^/(administration|sitemap)/ [NC,OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# Dync links
RewriteRule ^([a-zA-Z0-9-]+)/lang:([0-9]+)?$    index.php?t=$1&l=$2 [L]
RewriteRule ^lang:([0-9]+)?$                    index.php?l=$1 [L]

RewriteRule ^sitemap/?$                         index.php?sitemap=sitemap [L]

RewriteRule ^error/([0-9]+)?$                   index.php?error=$1
RewriteRule ^([a-zA-Z0-9-]+)/?$                 index.php?t=$1 
0

精彩评论

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