我需要帮助在我的新PHP网站的htaccess文件中添加301重定向。

huangapple go评论51阅读模式
英文:

I need help placing a 301 redirect in my htaccess file of my new php site

问题

以下是您要翻译的内容:

我有一个php网站,我的当前`.htaccess`文件如下:

    <IfModule mod_rewrite.c>

    #Options +FollowSymlinks


    RewriteEngine On


    RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-f

    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$ /index.php?page=$1 [NC,L,QSA]


    Options -Indexes


    </IfModule>


现在,我想在一个URL上进行301重定向。从`example.com/folder/old-url-london.htm`到`example.com/new-url-london/`

问题是,每次我尝试某事时,我会得到:

    www.example.com/new-url-london/?page=folder/old-url-london.htm

现在,我无法更改这一行(`RewriteRule ^(.*)$ /index.php?page=$1 [NC,L,QSA]`),因为它对网站的正常运行至关重要。

有什么想法吗?

我尝试了以下内容:

    Redirect 301 /folder/old-url-london.htm example.com/new-url-london/

以及

    RewriteRule ^folder/old-url-london.htm$ /new-url-london/ [R=301,L]
英文:

I have a php website and my current .htaccess file is as follows:

<IfModule mod_rewrite.c>

#Options +FollowSymlinks


RewriteEngine On


RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ /index.php?page=$1 [NC,L,QSA]


Options -Indexes


</IfModule>

Now I want to do a 301 redirect on a URL. From example.com/folder/old-url-london.htm to example.com/new-url-london/

The problem is everytime I try something I get:

www.example.com/new-url-london/?page=folder/old-url-london.htm

Now I can't change this line (RewriteRule ^(.*)$ /index.php?page=$1 [NC,L,QSA]) as it is essential to the working of the website.

Any ideas?

I tried the following:

Redirect 301 /folder/old-url-london.htm example.com/new-url-london/

as well as

RewriteRule ^folder/old-url-london.htm$ /new-url-london/ [R=301,L]

答案1

得分: 0

听起来你可能是把规则放错了地方。或者使用了Redirect指令。或者现在可能看到了缓存响应。

你需要使用mod_rewrite的RewriteRule来避免与现有的重写冲突,而且规则需要放在文件的顶部,在现有的重写之前,最好在RewriteEngine指令之后。

例如:

Options +FollowSymlinks -Indexes

RewriteEngine On

RewriteRule ^folder/old-url-london\.htm$ /new-url-london/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?page=$1 [QSA,L]

在测试之前,你需要清除浏览器缓存,因为浏览器可能已经缓存了任何错误的301(永久)重定向。出于这个原因,首先使用302(临时)重定向进行测试。

不要忘记在正则表达式中用反斜杠转义字面点号。

我整理了一些其他部分:

  • 不需要<IfModule>包装。
  • Options组合在顶部,并重新启用FollowSymLinks(因为mod_rewrite需要它)。
  • 在你发布的指令中,不需要RewriteBase
  • 删除了不必要的空格。
  • 重写中不需要NC标志,因为模式.*已经匹配所有内容。
  • 模式^(.*)$上的锚点是不需要的,因为正则表达式默认是贪婪的。
英文:

Sounds like you are perhaps putting the rule in the wrong place. Or used the Redirect directive. And/or are perhaps now seeing a cached response.

You need to use mod_rewrite RewriteRule to avoid conflicts with the existing rewrite AND the rule needs to be at the top of the file, before the existing rewrite and ideally after the RewriteEngine directive.

For example:

Options +FollowSymlinks -Indexes

RewriteEngine On

RewriteRule ^folder/old-url-london\.htm$ /new-url-london/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?page=$1 [QSA,L]

You will need to clear your browser cache before testing, since any erroneous 301 (permanent) redirects will have been cached by the browser. Test first with 302 (temporary) redirects for this reason.

Don't forget to backslash-escape literal dots in the regex.

I tidied up a few other bits:

  • <IfModule> wrapper is not required.
  • Combined Options at the top and re-enabled FollowSymLinks (since it is required by mod_rewrite)
  • RewriteBase is not required in the directives you've posted.
  • Removed spurious spacing.
  • NC flag not required on the rewrite since the pattern .* already matches everything.
  • The anchors on the pattern ^(.*)$ are not required since regex is greedy by default.

huangapple
  • 本文由 发表于 2023年2月19日 05:34:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75496529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定