显示规范重写的URL并隐藏动态URL

时间:2015-08-01 13:56:16

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

我尝试过无数的东西,但我找不到在浏览器网址栏中显示重写网址的方法,而不是使用3条获取参数显示的网址。

我的丑陋"网址:

http://example.com/archive.php?id=tt2131523&date=20131004&title=graceland

"漂亮"我需要显示的网址:

http://example.com/tt2131523-20131004-graceland.html

这是我的.htaccess文件:

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301]

RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301]

RewriteRule ^([^-]*)-([^-]*)-([^-]*)\.html$ /archive.php?id=$1&date=$2&title=$3 [NC,L]

我的其他PHP文件服务于"丑陋"链接,链接工作,但URL保持"丑陋"。 "漂亮的"版本不显示。

如果我输入"漂亮"浏览器中的URL,重写工作,我访问页面。

我希望这样,即使用户输入"丑陋"版本,他将被重定向到"漂亮"之一。

我是否需要更改我的链接结构(在其他php文件中)并使它们直接指向URL的重写版本,或者.htaccess中是否有显示重写版本的方法?

顺便问一下,旗帜好吗?

感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用:

RewriteEngine On
RewriteCond %{THE_REQUEST} /archive\.php\?id=([^&]+)&date=([^&]+)&title=([^\s]+) [NC] 
RewriteRule ^ /%1-%2-%3.html? [NC,R,L]
RewriteRule ^([^-]+)-([^-]+)-([^.]+)\.html$ /archive.php?id=$1&date=$2&title=$3 [QSA,L,NC]
相关问题