Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 将domain.tld/index.php重定向到domain.tld(删除index.php,然后删除301)_Regex_.htaccess_Magento_Mod Rewrite_Redirect - Fatal编程技术网

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

Regex 将domain.tld/index.php重定向到domain.tld(删除index.php,然后删除301),regex,.htaccess,magento,mod-rewrite,redirect,Regex,.htaccess,Magento,Mod Rewrite,Redirect,我安装了Magento,想知道如何将domain.tld/index.php重定向到domain.tld 我想删除index.php,然后将301返回到root。我有很多旧链接,这些链接给了我重复的内容和丑陋的URL等。旧链接示例: domain.tld/index.php?controller=best_sales 在.htaccess中,我有以下内容,但它不起作用 RewriteRule .* index.php [L] 谢谢:) 编辑: 谢谢你们两位的回答,但我还是无法让它发挥作用。

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

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

domain.tld/index.php?controller=best_sales
在.htaccess中,我有以下内容,但它不起作用

RewriteRule .* index.php [L]
谢谢:)


编辑:

谢谢你们两位的回答,但我还是无法让它发挥作用。 我得到404-没有找到,当试图作出任何改变

这是我的完整.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]

将此规则保留为您的第一条规则
行上的
重写引擎,以从URL中删除
index.php

DirectoryIndex index.php

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

我假设在没有任何规则的情况下,它已经为
域显示了您的内容。tld/?controller=best\u unicorns
。如果查询字符串前的url以“index.php”结尾,则需要重定向到该url,而不使用index.php。事实上,域名和查询字符串之间的部分只包含“index.php”

您需要为此使用
R
(重定向)标志:

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

删除您的其他尝试。如果此规则有效,请将标志更改为
[R=301,L]
以使其永久化。这样,浏览器和搜索引擎都知道只在一个地方查找。

谢谢anubhava!正在获取404,请查看我的答案以了解更多信息。You.htaccess未显示我的建议规则。您需要在
RewriteBase/
行下方插入我的建议规则。似乎工作正常:)如果我想去掉查询字符串,我可以在根斜杠后以某种方式添加问号吗?如果我只需要在根级别上使用它,而不是在子目录中使用它,这可能吗?是的,在末尾添加一个问号,比如
RewriteRule^(.*)index\.php$/$1?[L,R=302,NC,NE]
删除查询字符串。您也可以在root.htaccess中使用此规则。再次感谢。该规则只在根目录下运行,因为我担心删除查询字符串会破坏子目录中的函数和URL。谢谢Sumurai8!获取404,请查看我的答案以了解更多信息。确保此规则列为第一条规则。如果问题仍然存在,请找出它实际试图在服务器上加载的url。找出它应该加载的url。由于上一条规则,我的方法可能会导致无限循环,除非用
DirectoryIndex.php
替换。Anubhava的答案应该是可行的,即使当一切都按预期运行时,您应该用永久重定向来替换它。