Apache 组合Mod_重写规则
我有四个网址,我想重写Apache 组合Mod_重写规则,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我有四个网址,我想重写 domain.com/index.php?id=1234=>domain.com/1234 domain.com/a.php?id=1234=>domain.com/a/1234 domain.com/b.php?id=4321=>domain.com/b/4321 domain.com/gallery.php=>domain.com/gallery 其中,index检查数据库并重定向到正确的页面(a、b、c.php-ect),如果找不到有效ID,则重定向到gallery
RewriteEngine On
RewriteCond %{REQUEST_URI} !/gallery$ #excludes case 4
RewriteRule ^(.*)$ index.php?id=$1 #handles case 1
RewriteRule ^(.*)/(.*)$ $1.php?id=$2 #handles cases 2 and 3
RewriteCond %{REQUEST_URI} /gallery$
RewriteRule ^/gallery$ /gallery.php [L] #handles case 4
这会导致所有内容重定向到domain.com/gallery.php,并在尝试过多重定向时出错。即使我输入domain.com/a/1234,也会被重定向到domain.com/a/gallery.php
如何更好地分离这些案例-如果在案例1之后匹配domain.com/1234 stop-如果在案例2和案例3之后匹配domain.com/a/1234 stop-如果在调用domain.com/gallery.php之后匹配domain.com/gallery stop
谢谢大家的帮助 用以下代码替换您的代码:
RewriteRule ^gallery/?$ /gallery.php [L,NC] #handles case 4
RewriteRule ^([^/]+)/([^/]+)/?$ /$1.php?id=$2 [L,QSA] #handles cases 2 and 3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?id=$1 [L,QSA] #handles case 1
将代码替换为以下内容:
RewriteRule ^gallery/?$ /gallery.php [L,NC] #handles case 4
RewriteRule ^([^/]+)/([^/]+)/?$ /$1.php?id=$2 [L,QSA] #handles cases 2 and 3
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?id=$1 [L,QSA] #handles case 1
你是个救生员!太棒了!所以,让我确保我理解这里的逻辑:RewriteCond只适用于下一个RewriteRule?所以,如果我需要额外的重写条件来允许css、图像和字体之类的东西,我必须在每个重写规则之前放置这些重写条件?不客气。是的,RewriteCond确实只适用于下一个RewriteRule。你是个救生员!太棒了!所以,让我确保我理解这里的逻辑:RewriteCond只适用于下一个RewriteRule?所以,如果我需要额外的重写条件来允许css、图像和字体之类的东西,我必须在每个重写规则之前放置这些重写条件?不客气。是的,RewriteCond确实只适用于下一个RewriteRule。