【问题标题】:redirect non www to www and rewriterule将非 www 重定向到 www 并重写
【发布时间】:2016-06-01 18:33:33
【问题描述】:

事实上,我正在编写一个小的 PHP 脚本。我创建了一些规​​则来将非 www 重定向到 www 版本这是我的代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule app-([0-9]+)\.html app.html?id=$1 [L,QSA] 
AddType application/x-httpd-php5 .html .htm

重定向主页时它工作正常,但是当我想重定向 site.com/app-9.html 例如它只是重定向到 www 版本,如下所示: www.site.com/app.html?id=9。 它应该将 site.com/app-9.html 重定向到 www.site.com/app-9.html。请问我怎样才能做到这一点?

通过:

site.com/app-9.html

当前结果:

www.site.com/app.html?id=9

要求的结果:

www.site.com/app-9.html

【问题讨论】:

  • 如果不需要,为什么要添加RewriteRule app-([0-9]+)\.html app.html?id=$1 [L,QSA]
  • @RohitGoyani 请检查更新版本!
  • 如果不需要,您应该删除 RewriteRule app-([0-9]+)\.html app.html?id=$1 [L,QSA]。之后工作正常。
  • @RohitGoyani 这是必需的:)
  • 我们可以只为app-9.html 停止这条规则吗?意味着它永远不会重定向到app.html?id=9

标签: php apache .htaccess redirect mod-rewrite


【解决方案1】:

仅添加了对 app-9.html 的验证,因为其他人需要该规则。

使用这个:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

## added validation for app-9.html only
RewriteCond %{REQUEST_FILENAME}  !app-9.html -f
RewriteRule app-([0-9]+)\.html app.html?id=$1 [L,QSA] 

AddType application/x-httpd-php5 .html .htm

【讨论】:

    【解决方案2】:

    你能插入吗

    重写规则 ^/page /page?

    进入你的重写规则?这将完全删除查询字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-06
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 2019-03-09
      • 2021-08-03
      相关资源
      最近更新 更多