我启用了mod重写来删除所有页面扩展....
这是在httpd.conf中完成的,因为我在windows上使用apache。
我的设置是
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>这使得domain.com/bookings.html
显示为domain.com/bookings
我在html文件中启用了php,因此它可以解析所有页面中的php。
但我现在已经在我的网站上放了一个搜索框,如果我搜索“音乐”,它将使用url:
domain.com/search?q=music我希望我的urls看起来像这样:
domain.com/search/music但同时,我希望能够输入
domain.com/search/abba它会加载页面"search.html“,我们称之为"search”,它会添加参数?q=abba,但在地址栏中看起来仍然像上面的示例
我确信这可以通过mod重写来实现,但我不确定如何表达这个表达式。
预先感谢我收到的任何帮助:)
发布于 2012-02-08 04:42:29
使用html中的URL类型:domain.com/search/music
将其添加到DocumentRoot中的.htaccess文件中。
Options +FollowSymLinks +Indexes -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} (search)/([\w\d]+) [NC]
RewriteRule ^ %1.html?q=%2 [L,QSA]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]或者将其添加到您的.conf文件中
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{REQUEST_URI} (search)/([\w\d]+) [NC]
RewriteRule ^ %1.html?q=%2 [L,QSA]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>发布于 2012-02-08 05:02:29
您只需添加一个简单的RewriteRule:
RewriteRule ^/search/([a-zA-Z0-9]+)$ /search?q=$1 [QSA,NC,L]所以总而言之:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^/search/([a-zA-Z0-9]+)$ /search?q=$1 [QSA,NC,L]
</IfModule>哦,顺便说一下:
的维基
如果这还不够:
两个提示:
如果你不是在托管环境中 (=如果它是你自己的服务器,并且你可以修改虚拟主机,而不仅仅是.htaccess文件),试着使用RewriteLog指令:它可以帮助你追踪到这样的问题:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On我最喜欢用来检查regexp的工具:
http://www.quanetic.com/Regex (别忘了选择ereg(POSIX)而不是preg(PCRE)!)
https://stackoverflow.com/questions/9183436
复制相似问题