简体   繁体   中英

Making searches look like html pages with mod_rewrite

I want my urls that currently look like,

http://www.mydomain.com/search.php?q=blue+widgets

too look like this,

http://www.mydomain.com/blue+widgets.html

The code below makes the urls look like this,

http://www.mydomain.com/blue+widgets/

How can I get it to add .html at the end? I tried several things and haven't been able to get it to work.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/(.*) search.php?q=$1 [L,QSA]

You almost had it:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+).html$ search.php?q=$1 [L,QSA]

([^.]+) is the frist group and means anything not a dot
.html is the left over to match the extension you want

Since you're using this on .htaccess you don't need the /?

Just add the html to the pattern:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)\.html$ search.php?q=$1 [L,QSA]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM