regex - 将 domain.tld/index.php 重定向到 domain.tld(删除 index.php,然后删除 301)

标签 regex .htaccess magento mod-rewrite redirect

我安装了 Magento,想知道如何将 domain.tld/index.php 重定向到 domain.tld?

我想删除 index.php 然后 301 回到 root。我有很多旧链接,这些链接给我重复的内容和丑陋的网址等。旧链接示例:

domain.tld/index.php?controller=best_sales

在 .htaccess 中,我有以下内容,但它不起作用

RewriteRule .* index.php [L]

谢谢:)


编辑:

感谢你们两位的回答,但我仍然无法让它工作。 尝试进行任何更改时,我收到 404 - Not Found。

这是我的完整 .htaccess,也许有一些错误或配置错误导致它无法正常工作?

Options +FollowSymLinks
RewriteEngine on    
RewriteBase /

// REMOVE HOME REWRITE FROM MAGENTO
RewriteRule ^home/?$ /? [R=301,L,NC]

// ADD WWW TO NONE WWW FOR BOTH HTTPS AND NONE HTTPS
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

// REDIRECT ALL .HTML FILES AND ALL .HTML/ FILES WITH TRAILING SLASH
RewriteRule ^google[0-9a-f]+.html$ - [L]
RewriteRule (.+)\.html$ /$1/ [L,R=301]
RewriteRule (.+)\.html\/$ /$1/ [L,R=301]

// ADD TRAILING SLASH
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

// TRAILING SLASH CHECK
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]

// CHECK IF REDIRECT POINTS TO A VALID FILE
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

// SEND TO INDEX.PHP FOR CLEAN URLS
#RewriteRule ^(.*)$ index.php? /$1 [L,QSA]
#REWRITE EVERYTHING ELSE TO INDEX.PHP    
RewriteRule .* index.php [L]

最佳答案

将此规则作为您的第一条规则保存在 RewriteEngine On 行下方,以从您的 URL 中删除 index.php:

DirectoryIndex index.php

RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]

关于regex - 将 domain.tld/index.php 重定向到 domain.tld(删除 index.php,然后删除 301),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25131700/

相关文章:

javascript - 我必须使用 RegEx 来验证表单的多个部分

python - 如何使用 Python 从现有列表中重命名已抓取的文件

php - 重定向 POST htaccess

apache - 重写规则生成器?

php - Magento 配置具有 100 种排列的产品

magento - 编辑特定类别的目录页面

Magento WS-I 兼容 v2 API WSDL Web 服务 SOAP-ERROR : Encoding: object has no 'sessionId' property

javascript - 删除某些跨度标签,将文本保留在里面 - Javascript/jquery

regex - VBA正则表达式问题

php - 删除 CI ubuntu 12.04 中的 index.php 文件