简体   繁体   中英

.htaccess rewrite rule of dynamic url

I got the following dynamic url from the database :

http://mydomain.com/download.php?id=1234&name=title-of-this-download&category=windows

This url has to be permanent rewrited to:

http://mydomain.com/windows/title-of-this-download

What rewrite rules should there be in the .htaccess file?

So far I came up with this:

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

But it rewrite anything. Thanks in advance.

You're going to be missing the ID, because it's not part of the permanent URL. With no ID in the URL, mod_rewrite isn't going to know what to fill in for the $_GET['id'] get parameter, so the best you can do is:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /download.php?name=$2&category=$1 [L]

and may also need

RewriteCond %{THE_REQUEST} \ /+download\.php\?(id=[0-9a-f]+&)?name=([^&]+)&category=([^&\ ]+)
RewriteRule ^ /%3/%2? [L,R=301]

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