简体   繁体   中英

Rewrite rule on multiple variables using .htaccess

I am pretty rookie on that and would like to know how to rewrite with .htaccess an url http://www.example.com/index.php?lang=21&page=164 to say http://www.example.com/de/value-added-tax/ , where "de" refers to german language and "value-added-tax" refers to page id no 64. I also have css and image files that need to keep on working (eg ./img/logo.png). There are couple of other *.php files too that need to keep on working (eg search.php). The hosting server is Apache.

You cannot rewrite as you ask ... sorry, Apache does not have access to your database . You cannot ask it to query on the names and giving back the IDs ...

But maybe you can try something like this :

Options +FollowSymlinks
RewriteEngine on

RewriteCond $1 !^(search\.php|img/|robots\.txt)
RewriteRule ^([a-z]{2})/([a-zA-Z0-9-]*)(/?)$ index.php?lang=$1&page=$2 [L]

This will work with mydomain.tld/xx/zzz-zzz and mydomain.tld/xx/zzz-zzz/ This will redirect everything like that, except if it starts with search.php , or img/ or robots.txt

  • xx = 2 letters in lowercase
  • zzz-zzz = All alphanumerics and the -

This will work with trailing slash, and without it at the end of the address

But you have to modify your script to search by page name and no more by page id, same for languages

Hope it helped !

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