【问题标题】:Mod_Rewrite: Rewrite URL to index.php but redirect index.php to rootMod_Rewrite:将 URL 重写为 index.php,但将 index.php 重定向到根目录
【发布时间】:2015-02-26 07:04:57
【问题描述】:

我想重写请求以使用非常标准且有效的 index.php。

问题是包含 index.php 的旧链接现在无效,我想将它们重定向到我网站的根 url。

可能的重复项包括:Rewrite URL to index.php but avoid index.php in the URL,但提到的答案对我不起作用。

RewriteEngine on

# This results in a redirect loop
#Redirect 301 /index.php http://domain.com

# This gets me a 500 error
# RewriteCond %{THE_REQUEST} ^GET\ /index\.php/?([^ ]*)
# RewriteRule ^index\.php/?(.*) / [L,R=301]

# Push www traffic to base domain.  <<= Works
RewriteCond %{HTTP_HOST} !^domain.com$ [NC] 
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

# Final rewrite to include index.php   <<= Works
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /?([A-Za-z0-9_-]+)/?$ index.php?id=$1 [QSA,L]

网址:http://domain.com/index.php/component/fsf/?view=faq,应该是无效的,它应该返回一个 404 错误,但由于我当前的重写规则,网站访问者将看到主页。我想用 301 将它重定向到主页,以便可以从搜索索引中删除它。 任何帮助将不胜感激。

【问题讨论】:

    标签: mod-rewrite


    【解决方案1】:

    将您的 THE_REQUEST 规则更改为

    RewriteCond %{THE_REQUEST} ^GET\ /index\.php(/.*) [NC]
    RewriteRule ^ /? [L,R=301]
    

    【讨论】:

    • 更接近,它也重写了具体示例 [dbfront.com/?view=faq] 并且没有引起任何进一步的问题。我实际上希望 URL 中没有任何垃圾。