2016-12-30 92 views
0

如何这个网址:URL Rewrting规则

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 '/') 
+0

,看一下http://stackoverflow.com/questions/13003319/htaccess-rewrite-query-string-as-path –

回答

0

试试这个:

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 
+0

谢谢您回答。但如果有其他的东西,而不是像http://www.domain.com/whatever.php的服务,不管http://www.domain.com/whatever/whatever什么,因为这个网址是动态的所有网页,所以那里应该是所有这些页面的通用解决方案。 –

+0

我会更新答案以反映这一点。 – sepehr

+0

谢谢你快速回答。但这不适用于我...我已经使用了这两个选项,但无法获得理想的结果。我已经更新了上面的问题。好心检查! –