【问题标题】:htaccess rewrite clean url for multiple parametershtaccess 为多个参数重写干净的 url
【发布时间】:2015-09-27 05:54:21
【问题描述】:

这是我的 .htaccess 代码,用于干净的 url 重写。

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[a-zA-Z]{3,}\s/+index\.php\?bank=([^\s&]+) [NC]
RewriteRule ^ %1%2%3%4? [R=301,L]
RewriteRule ^([^/]+)/?$ index.php?bank=$1$2$3$4 [L,QSA]

此代码适用于url 中的单个参数。比如rewrites

http://example.com/example-path/
http://example.com/index.php?bank=example-path/

但是当我尝试在url 中传递多个参数时,我得到的只是errors

我需要一个类似
http://example.com/example-path 的网址,而不是http://example.com/index.php?bank=example-path

如何更改我的代码以传递多个 url。

【问题讨论】:

  • 使用临时 302 重定向而不是永久 301 重定向进行开发。 301 重定向适用于网站永久移动,例如更改域名。但是当涉及到 URL 重写时,您会发现即使在生产环境中也不断地移动内容,并且由于浏览器缓存,301 重定向会给您带来麻烦。尝试切换到 302 重定向,然后在没有缓存重定向的其他浏览器中继续测试,或者从 301 重定向中清除浏览器缓存。

标签: apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

您的重定向逻辑可以大大简化为:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

#If a request does not match an existing directory
  RewriteCond %{REQUEST_FILENAME} !-d
#And does not match an existing file
  RewriteCond %{REQUEST_FILENAME} !-f
#then rewrite all requests to index.php
  RewriteRule ^(.*)/?$ index.php?bank=$1 [L,QSA]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-25
    • 2015-11-01
    • 1970-01-01
    • 2016-04-24
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    相关资源
    最近更新 更多