【问题标题】:.htaccess redirect variable to friendly link - many redirects.htaccess 将变量重定向到友好链接 - 许多重定向
【发布时间】:2017-06-15 12:32:49
【问题描述】:

我在 htaccess 将变量重定向到清理 url 时遇到问题。

代码:

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

RewriteCond %{QUERY_STRING} ^page=about$ 
RewriteRule ^(.*)$ https://www.url.com/about? [R=301,L]

我只想将这个:“?page=about”重定向到这个:“/about”

上面的代码给了我很多重定向...

感谢您花时间看这个。

【问题讨论】:

  • 你必须看到这篇文章:stackoverflow.com/questions/1231067/…
  • 那个解决方案没有重定向,我上面有那个解决方案,但不能正常工作......
  • 第一个“RewriteRule”给我友好的网址。但第二个重定向到我想要的 /about 但它给了我很多重定向,我不知道如何解决这个....

标签: .htaccess variables redirect clean-urls


【解决方案1】:

所以我在本地服务器上的一个空目录中尝试了你的代码。

这很好用:

<IfModule mod_rewrite.c>
    RewriteEngine on

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

    RewriteCond %{QUERY_STRING} ^page=about$ 
    RewriteRule ^(.*)$ https://www.url.com/about? [R=301,L]
</IfModule>

但当我注释掉这行代码RewriteEngine on 时不会。所以用这段代码试试你的代码,它应该可以正常工作。

【讨论】:

  • 抱歉,还是会跳转到错误页面“ERR_TOO_MANY_REDIRECTS”
  • 您能粘贴完整的.htaccess 代码吗?此外,检查上述重定向之后发生的任何其他重定向。
【解决方案2】:

这就是完整的代码。

#Rewrite settings
Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^url.com [NC]
RewriteRule ^(.*)$ https://www.url.com/$1 [L,R=301]

#Remove index.php from url
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?p=$1 [L]

#RewriteCond %{QUERY_STRING} ^p=about$ 
#RewriteRule ^(.*)$ https://www.url.com/about? [R=301,L]

RewriteCond %{HTTP_USER_AGENT} libwww-perl.* 
RewriteRule .* ? [F,L]

这段代码被注释掉是因为这样不行:

#RewriteCond %{QUERY_STRING} ^p=about$ 
#RewriteRule ^(.*)$ https://www.url.com/about? [R=301,L]

【讨论】:

    猜你喜欢
    • 2020-02-09
    • 1970-01-01
    • 2014-02-24
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多