.htaccess重写以生成干净的URL

时间:2021-12-24 10:38:45

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

目前我设置了这样的重写规则,以生成干净简单的url。

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

目前,about页面位于index.php?a = about&b = user,遗憾的是这不能改变。正如您所看到的,它在浏览器地址栏中看起来并不是很好。

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

我正在使用phpdolphin的预制脚本,它运行正常。但是url都是index.php的基础,我想清理它们

Currently the only code within the .htaccess file

目前.htaccess文件中唯一的代码

RewriteEngine on

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

3 个解决方案

#1


0  

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]

#2


0  

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

当用户点击index.php?a = about&b = user时,您可以添加此RewriteRule来重定向请求

    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:

或者您可以在index.php中使用php header()函数来重定向请求:

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

#3


0  

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

我喜欢使用这个代码,因为它很短并且匹配除了〜之外的所有内容,这在网址中非常罕见

#1


0  

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]

#2


0  

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

当用户点击index.php?a = about&b = user时,您可以添加此RewriteRule来重定向请求

    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:

或者您可以在index.php中使用php header()函数来重定向请求:

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

#3


0  

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

我喜欢使用这个代码,因为它很短并且匹配除了〜之外的所有内容,这在网址中非常罕见