有没有办法通过htaccess通过删除URL的parat来更改URL来清理URL?

时间:2014-03-30 01:20:55

标签: .htaccess clean-urls

我的所有网址格式都是这样的:

http://example.com/category?format=feed
http://example.com/category/sub_category/?format=feed

例如:http://example.com/computer/cpu/?format=feed

是否有办法通过htaccess将热门网址更改为:

http://example.com/category/feed
http://example.com/category/sub_category/feed

example1:http://example.com/computer/cpu/feed

example2:http://example.com/computer/feed

2 个答案:

答案 0 :(得分:2)

这些规则应满足这两个要求。但是,只要普通URL不变,format始终是查询字符串中的键,您就可以更改类别和子标记。

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.+)/(.+)$ /$1/$2/?format=$3 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/(.+)$ /$1/?format=$2 [L]

然后您应该能够使用这些URL类型。

http://example.com/category/feed
http://example.com/category/sub_category/feed

答案 1 :(得分:0)

以下内容可以实现您的目标:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^format=(.*)$
RewriteRule ^(.*)/(.*)/$ /$1/$2/%1? [L,R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^format=(.*)$
RewriteRule ^(.*)$ /$1/%1? [L,R]

以上内容将转为:

http://example.com/category?format=feedhttp://example.com/category/feed

http://example.com/category/sub_category/?format=feedhttp://example.com/category/sub_category/feed