简体   繁体   中英

.htaccess rewrite to produce clean url

Currently I set a rewrite rule like so, to produce clean and simple url's.

.htaccess

RewriteRule ^/about$  about.php  [L]

But what i need to do is something a little different, the other way around. For example if a user clicks the following link

<a href="index.php?a=page&b=about">about</a> 

They would go to the about page /about

Currently the about page resides at index.php?a=about&b=user and this can't be changed unfortunately. As you can see it does not look very nice in the browser address bar.

EDITED

I am using a pre-made script from phpdolphin , which is working fine. But the url are all index.php based and i would like to clean them up

Currently the only code within the .htaccess file

RewriteEngine on

RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$  index.php?a=$1&q=$3    [L]

Add this rule before your existing rule:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?a=([^&]+)&b=user[\s&] [NC]
RewriteRule ^ /%1? [R=301,L]

RewriteRule ^([^/.]+)/?$ index.php?a=$1&b=user [L,NC,QSA]

You can add this RewriteRule to redirect the request when user hit index.php?a=about&b=user

    RewriteCond %{QUERY_STRING}         ^(.*)a=about(.*)$    [NC]
    RewriteRule ^index.php        /about [L,NC,R=301]

or you can use php header() function in index.php to redirect the request:

if ($_REQUEST['a'] == about) {
    header('Location: /about');
    exit;
}

Try this i checked and it works ...

RewriteEngine On

RewriteRule    ^([A-Za-z0-9-+_%*?]+)/([A-Za-z0-9-+_%*?]+)/?$    index.php?a=$1&q=$2     [L]

Note: add additional signs between [] if necessary

OR This

RewriteRule    ^([^~]+)/([^~]+)/?$    index.php?a=$1&q=$2

I like to use this code because it's short and matches everything except for ~ which is very very rare to see in a url

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM