【问题标题】:Is there a way via htaccess to change URLs to clean URLs by deleting a parat of an URL?有没有办法通过 htaccess 通过删除 URL 的一部分来更改 URL 以清理 URL?
【发布时间】:2014-03-30 01:20:55
【问题描述】:

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

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

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

有没有办法通过 htaccess 将顶级 URL 更改为这些:

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

示例 1:http://example.com/computer/cpu/feed

例子2:http://example.com/computer/feed

【问题讨论】:

  • 是的,有办法。请参阅右侧的长列表以获取更多信息-->

标签: .htaccess clean-urls


【解决方案1】:

这些规则应同时满足这两个要求。但是只要正常的 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

【讨论】:

  • 感谢您的回答...干净的 URL 只能在内部更改...对吗?但不会发生这种情况!
  • 是的,像 http://example.com/category/feed 这样的干净 URL 应该显示在地址栏中。如果您使用上面的示例代码,它应该只在内部重定向。这个[L] 告诉它在匹配规则时停止处理。确保这就是你所拥有的一切。不要使用R
【解决方案2】:

以下应该实现您正在寻找的:

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

【讨论】:

    猜你喜欢
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多