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

Mod rewrite mod_rewrite-如何使两个规则一起工作,mod-rewrite,combinations,Mod Rewrite,Combinations,我一直在尝试为我的网站创建友好的URL,并遵循以下规则: #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

我一直在尝试为我的网站创建友好的URL,并遵循以下规则:

#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

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

这条规则将匹配任何以单斜杠结尾的URL(但不是双斜杠),因此它将阻止您访问上一条规则

RewriteRule ^(.+[^/])/$ /index.cfm?page=$1 [QSA,L]
我怀疑你需要的是以下几点:

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

为了简单起见:您可以将大部分的
RewriteCond
打包在一起:
RewriteCond%{REQUEST\u FILENAME}-f RewriteCond%{REQUEST\u FILENAME}-d RewriteRule.-[l]
感谢KingCrunch的建议,这是一个全局规则,所以我不必继续使用RewriteCond%{REQUEST\u FILENAME}-f RewriteCond%{REQUEST_FILENAME}-对于每一条规则,它都是对你现在所做的事情的否定:你说“如果X不是文件而不是目录,那么就做Y”(4次)。我说“如果x是文件或目录,什么也不做”,所以您应该只需要它一次
RewriteCond%{REQUEST\u FILENAME}-f[或]RewriteCond%{REQUEST\u FILENAME}-d重写规则。*-[L]
(我忘记了
[或]
-标志;)。它应该使它更具可读性,但我不能完全确定它是否会干扰其他规则/条件。啊,我明白了,谢谢你的解释。我会把这个加进去。你对我的主要问题有什么建议吗?谢谢mootinator,这帮助我找到了我想要的解决方案。