【问题标题】:URL Rewrting RulesURL重写规则
【发布时间】:2016-12-30 17:59:12
【问题描述】:

这个网址如何:

www.domain.com/services.php?hello-world-be-active

可以改写为:

www.domain.com/services/hello-world-be-active

更新:

以下规则不起作用...我正在使用此规则

RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php [NC]
RewriteRule ^ /%1 [QSA,R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php

上述规则给出以下结果:

www.domain.com/whatever.php => www.domain.com/whatever (Correct)
www.domain.com/whatever.php?whatever-whatever => www.domain.com/whatever?whatever-whatever (Not Correct because question mark '?' should replace with slash '/')

【问题讨论】:

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


【解决方案1】:

试试这个:

Options +FollowSymLinks

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /

    # Match the host, if you want
    RewriteCond %{HTTP_HOST} ^www\.domain\.com$

    RewriteCond %{REQUEST_URL} !\.php$
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^[^/]*/(.*)$ %{REQUEST_FILENAME}.php?$1 [QSA,L]
</IfModule>
## Results:
# services/whatever-here         => services.php?whatever-here
# services/whatever-here?foo=bar => services.php?whatever-here&foo=bar
# anything/whatever              => anything.php?whatever
# services/foo/bar               => services.php?foo/bar

有了这些规则,对services/whatever 的任何调用都将映射到services.php 文件。如果您将R flag 添加到重写规则的末尾,则请求将在外部重定向services.php

但如果你想反过来;意思是将services.php?whatever 重定向到services/whatever,这就是你需要的:

Options +FollowSymLinks

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /

    # Match the host, if you want
    RewriteCond %{HTTP_HOST} ^www\.domain\.com$

    RewriteRule (.+)\.php $1/%{QUERY_STRING} [R,QSD,L]
</IfModule>
## Results:
# foo/bar/baz.php?qux   => foo/bar/baz/qux
# services.php?whatever => services/whatever

【讨论】:

  • 感谢您的回答。但是如果有其他东西而不是像domain.com/whatever.php?whateverdomain.com/whatever/whatever 这样的服务,因为这个网址对于所有网页都是动态的,所以所有这些页面也应该有一个通用的解决方案。
  • 感谢您的快速回答。但这对我不起作用......我已经使用了这两个选项但无法获得预期的结果..我已经更新了我上面的问题。请检查!
猜你喜欢
  • 2018-11-21
  • 1970-01-01
  • 1970-01-01
  • 2014-10-27
  • 2011-12-06
  • 2017-02-03
相关资源
最近更新 更多