如何将多个查询字符串URL更改/重定向到简单的URL?

时间:2015-04-10 05:33:35

标签: apache .htaccess mod-rewrite

有些网址具有相同的网址结构模式:

//www.example.com/index.php?hop=this&step=that&fgh=1001
//www.example.com/index.php?hop=this&step=that&fgh=1002
//www.example.com/index.php?hop=this&step=that&fgh=1003

//www.example.com/index.php?hop=this&step=that&fgh=部分是相同的 1001~1003是唯一独特的部分。

我想将它们更改/重定向到:

http://www.example.com/fgh/1001
http://www.example.com/fgh/1002
http://www.example.com/fgh/1003

此站点使用mod_rewrite在Apache上运行 我曾尝试使用.htaccess文件重写规则,但到目前为止失败了。

我希望有人可以帮我改写.htaccess

中的代码

3 个答案:

答案 0 :(得分:0)

它应该适合你。我试一试。 把它放在.htaccess

RewriteEngine On
RewriteCond %{QUERY_STRING} ^hop=(.*)$ [NC,OR]
RewriteCond %{QUERY_STRING} ^step=(.*)$ [NC,OR]
RewriteRule (.*) /$1? [R=301,L]
RewriteRule index/fgh/(.*)/ index.php?fgh=$3
RewriteRule index/fgh/(.*) index.php?fgh=$3 

答案 1 :(得分:0)

您可以在DOCUMENT_ROOT/.htaccess文件中使用此代码:

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /index\.php\?hop=this&step=that&fgh=(\d+) [NC]
RewriteRule ^ /fgh/%1? [R=301,L]

RewriteRule ^fgh/(\d+)/?$ index.php?hop=this&step=that&fgh=$1 [QSA,L,NC]

将这两条规则放在RewriteEngine行的下方。

答案 2 :(得分:0)

让我添加.htaccess文件的主要部分;

RewriteEngine On

RewriteRule index\.php - [L]


#This is the code I tried but not working
RewriteCond %{QUERY_STRING} ^hop=this&step=that&fgh=([^&]+)$ [NC]
RewriteRule ^(index\.php)?$ /fgh/%1? [R=301,L,NC]


## End of deny access to extension xml files
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
# Block out any script trying to base64_encode crap to send via URL
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
# Block out any script that includes a <script> tag in URL
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Send all blocked request to homepage with 403 Forbidden error!
RewriteRule ^(.*)$ index.php [F,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]