简体   繁体   English

使用.htaccess重写多个变量的规则

[英]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). 我对此非常了解,并且想知道如何使用.htaccess重写网址http://www.example.com/index.php?lang=21&page=164并说http://www.example.com/ de / value-added-tax / ,其中“ de”是德语,“增值税”是页面id no64。我还有一些css和图像文件,需要继续使用(例如./img /logo.png)。 There are couple of other *.php files too that need to keep on working (eg search.php). 还有其他两个* .php文件也需要继续工作(例如search.php)。 The hosting server is Apache. 托管服务器是Apache。

You cannot rewrite as you ask ... sorry, Apache does not have access to your database . 您无法按要求进行重写...很抱歉, Apache无权访问您的数据库 You cannot ask it to query on the names and giving back the IDs ... 您不能要求它查询名称并返回ID ...

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 这将与mydomain.tld/xx/zzz-zzzmydomain.tld/xx/zzz-zzz/这将重定向所有类似内容,除非它以search.phpimg/robots.txt开头

  • xx = 2 letters in lowercase xx = 2个小写字母
  • zzz-zzz = All alphanumerics and the - zzz-zzz =所有字母数字和-

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 但是您必须修改脚本以按页面名称搜索,而不必按页面ID进行搜索,对于语言来说也一样

Hope it helped ! 希望能有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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