【发布时间】:2014-11-24 23:56:34
【问题描述】:
我想要一个这样的网址
“www.site.com/?u=1”转换成这个“www.site.com/1”
不影响我的 php 脚本。
我还想了解这种转换是如何以及何时在服务器上发生的。
这是我在查询字符串上没有运气的尝试。
Options -Indexes +FollowSymLinks
RewriteEngine On
DirectoryIndex index.php
#////////////Convert 'u' Query String////////////
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?u=$1 [NC]
#/////////////append www before all urls///////////////////////////
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
#///////////////////remove index.php from url/////////////
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
谢谢。
【问题讨论】:
-
我假设您网站上的任何网址都是这样的:
www.site.com/?u=products/listing。然后,您希望网站将用户重定向到www.site.com/products/listing。我的建议:您应该编辑 php 文件,以便浏览器不会在每次单击链接时通过 htaccess 重定向两次(1 次加载和 1 次重定向)。你想要的只是一个技巧,而不是解决方案。
标签: php .htaccess url url-rewriting query-string