mod_rewrite - 如何使两个规则一起工作

时间:2011-07-15 09:38:35

标签: mod-rewrite combinations

我一直在努力为自己的网站制作友好的网址,并遵守以下规则:

#enable rewrites
RewriteEngine on
RewriteBase /

# Redirect 404s
ErrorDocument 404 /404_not_found.cfm

# Redirect non www to www

RewriteCond %{HTTP_HOST} ^example.co.uk [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]

# Add trailing slashes to uri 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/(images|js|css)
RewriteRule ^([^/.]+)$ /$1/ [R=301,L,NC]

# Redirect any request with page var to /var/ format

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^page=(.+[^/])$ [NC]
RewriteRule ^index\.cfm$ http://%{HTTP_HOST}/%1/ [R=301,L,NC]

# If not an existing file or directory
# Rewrite any request var/ to index.cfm?page=var

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])/$ /index.cfm?page=$1 [QSA,L]

# property services

# Add trailing slash if not a file or directory and 301 redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^property-services/([^/.]+)$ http://%{HTTP_HOST}/property-services/$1/ [R=301,L,NC]

# Rewrite request any request with page var to property-services/var/
RewriteRule ^property-services/([^/.]+)/$ property-services/index.cfm?page=$1 [L,NC]

一切正常,接受最终规则:

# Rewrite request any request with page var to property-services/var/
RewriteRule ^property-services/([^/.]+)/$ property-services/index.cfm?page=$1 [L,NC]

我尝试请求时未加载页面:

http://www.example.co.uk/property-services/example-service/

如果我删除倒数第二个并且规则,那么我可以通过请求来加载页面:

http://www.example.co.uk/property-services/example-service

关键是我需要在末尾加载带斜杠的页面,因为这是我为网站选择的格式。有人可以解释我错在哪里吗?

1 个答案:

答案 0 :(得分:1)

此规则将匹配以单斜杠结尾的任何网址(但不是双斜杠),因此会阻止您到达上一条规则。

RewriteRule ^(.+[^/])/$ /index.cfm?page=$1 [QSA,L]

我怀疑你需要的是:

RewriteRule ^([^/]+)/$ /index.cfm?page=$1 [QSA,L]