Apache htaccess重定向与现有重写规则冲突
我将所有进入我网站的URL请求转换为GET params,因此Apache htaccess重定向与现有重写规则冲突,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,我将所有进入我网站的URL请求转换为GET params,因此fruits.com/apple变成fruits.com/?URL=apple,我的路由机制负责显示正确的视图 .htaccess 这个很好用。然而,我现在有一些例外,我希望这些网址重定向到主页重定向与上述规则冲突: Redirect /pineapple / Redirect /grapes / 这将重定向到fruits.com/?url=spineapple,而不仅仅是fruits.com 如何将文件重新定向到fruits.co
fruits.com/apple
变成fruits.com/?URL=apple
,我的路由机制负责显示正确的视图
.htaccess
这个很好用。然而,我现在有一些例外,我希望这些网址重定向到主页<代码>重定向与上述规则冲突:
Redirect /pineapple /
Redirect /grapes /
这将重定向到fruits.com/?url=spineapple
,而不仅仅是fruits.com
如何将文件重新定向到fruits.com
可怕的解决办法
我可以想出一个解决方案,除了几个例外,它对任何事情都非常不雅观:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteCond %{REQUEST_URI} !^/pineapple$
RewriteCond %{REQUEST_URI} !^/grapes$
RewriteRule .* index.php?url=$0 [QSA,L]
Redirect /pineapple /
Redirect /grapes /
在代码方面,我们不太关心优雅,而更关心性能
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteCond %{REQUEST_URI} !^/(pineapple|grapes)/?$
RewriteRule .* index.php?url=$0 [QSA,L]
RewriteRule (pineapple|grapes)/? / [R,L]
如果您有权访问
httpd.conf
,如果您关心它的外观,则可以使用。切换到带有R标志的重写规则,而不是重定向。您还将使用重写规则上的L标志来告诉Apache,如果此规则匹配,则停止处理其他规则。所以你把这个放在其他规则之上
RewriteEngine on
RewriteRule ^pineapple / [R=301,L]
RewriteRule ^apple / [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule .* index.php?url=$0 [QSA,L]
将代码替换为以下内容: 通过
httpd.conf
启用mod_rewrite和.htaccess,然后将此代码放入文档根目录下的.htaccess
:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^(pineapple|grapes)/? /? [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule .+ index.php?url=$0 [QSA,L]
很公平。解决方案中的重定向有效,但是URL中仍然可以看到/spineapple
和/grapes
。抱歉,忘记了硬重定向标志。很抱歉,这不起作用。它继续在fruits.com/菠萝/
上打字太快。在我应该使用^
的地方使用了/
。如果你想在任何地方而不仅仅是一开始就匹配apple,则需要使用^
来阻止你匹配fruits.com/subdir/apple
删除它。成功!我不需要选项
,但你的重写规则成功了。它现在正是我想要的工作方式,所以谢谢你。不客气,是的,我知道这个简单的案例不需要选项,但我仍然提供了它们,因为我试图提供完整的答案。
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^(pineapple|grapes)/? /? [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule .+ index.php?url=$0 [QSA,L]