.htaccess redirect variable to friendly link - many redirects

廉价感情. 提交于 2020-01-06 05:21:06

问题


I have a problem with htaccess redirecting a variable to clean url.

the code:

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]

i just want to redirect this: "?page=about" to this: "/about"

and with the code above give me to many redirects...

Thanks for the time to look at this.


回答1:


So i have tried your code in an empty directory on local server.

This works fine:

<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>

But not when i comment out this line of code RewriteEngine on. So try your code with this piece of line and it should work fine.




回答2:


Thats the complete code.

#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]

This piece of code is commented because like this does not work:

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


来源:https://stackoverflow.com/questions/44567637/htaccess-redirect-variable-to-friendly-link-many-redirects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!