简体   繁体   English

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

[英]Redirect domain.tld/index.php to domain.tld (remove index.php and then 301)

I have a Magento installation and was wondering how can I redirect domain.tld/index.php to domain.tld? 我有一个Magento安装,并想知道如何将domain.tld / index.php重定向到domain.tld?

I want to remove index.php and then 301 back to root. 我想删除index.php然后301返回root。 I have a lot of old links which is giving me duplicate content and ugly urls etc. Example of old link: 我有很多旧的链接,它给了我重复的内容和丑陋的网址等。旧链接的例子:

domain.tld/index.php?controller=best_sales

In the .htaccess, I have the following, and it's not working 在.htaccess中,我有以下内容,但它无效

RewriteRule .* index.php [L]

Thanks :) 谢谢 :)


Edit: 编辑:

Thanks both of you for your answers, but I still can't get it to work. 感谢你们两位的答案,但我仍然无法让它发挥作用。 I'm getting 404 - Not Found, when trying to make any changes. 在尝试进行任何更改时,我得到404 - Not Found。

This is my full .htaccess, maybe there are some errors or miss-configurations that doesn't make it work? 这是我的完整.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]

Keep this rule as your very first rule below RewriteEngine On line to remove index.php from your URL: 将此规则作为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]

I am assuming that without any rules it already displays your content for domain.tld/?controller=best_unicorns . 我假设没有任何规则它已经显示domain.tld/?controller=best_unicorns If the url before the query string ends with 'index.php' , you want to redirect to that url without index.php. 如果查询字符串前面的url 以'index.php'结尾 ,则需要重定向到不带index.php的url。 In fact, the part between the domain name and the query string only contains 'index.php'. 实际上,域名和查询字符串之间的部分只包含'index.php'。

You'll need to use the R (redirect) flag for this: 你需要使用R (重定向)标志:

RewriteRule ^index\.php$ / [R,L]

Delete your other attempt. 删除你的其他尝试。 If this rule works, change the flags to [R=301,L] to make it permanent. 如果此规则有效,请将标志更改为[R=301,L]以使其成为永久标志。 This way both browsers and search engines know to look in just one place. 这样,浏览器和搜索引擎都知道只在一个地方查看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM