.htaccess remove index.php and hide parameter key from URLs

橙三吉。 提交于 2019-12-23 10:28:52

问题


I have the following URL

www.example.com/index.php?tag= xxx

I want to make it like the following using .htaccess

www.example.com/xxx

I done it with this code:

 Options +FollowSymLinks -MultiViews
    # Turn mod_rewrite on
    RewriteEngine On
    RewriteBase /

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

SO if I input this URL:

www.example.com/index.php?tag=1000

it be redirect to:

www.example.com/?tag=1000

if: www.example.com/1000 it works!

So I have dublicate URL and it's not good for seo.

How can redirect www.example.com/index.php?tag=1000 to www.example.com/1000


回答1:


How can redirect www.example.com/index.php?tag=1000 to www.example.com/1000

You can insert this rule just below RewriteBase line:

RewriteCond %{THE_REQUEST} /(?:index\.php)?\?tag=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]



回答2:


use this code for .htaccess file

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

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

you index.php code will be

<?php 
echo $_REQUEST['key'];  
?>

then call http://sitename.com/1000

output will be :1000



来源:https://stackoverflow.com/questions/30153499/htaccess-remove-index-php-and-hide-parameter-key-from-urls

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