3

我有这个网站,我想重写 URL:

mydomain.com/referinte?lang=en

mydomain.com/referinte?lang=ro

有没有可能,所以我可以使用 .htaccess 来修改网址,如下所示:

mydomain.com/en/references

mydomain.com/ro/referinte

我当前的 .htaccess 是:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
4

1 回答 1

0

Based on the your input, I would suggest using URL rewriting configuration as follows:

RewriteEngine  on
RewriteRule    ^([a-zA-Z-_]+)$  $1.php?lang=en  [R]
RewriteRule    ^([a-z]+)/([a-zA-Z-_]+)$  $2.php?lang=$1  [R]

The first rule rewrites URLs like /abc (with no specified language) to /abc.php?lang=en and sets English (en) as a default language The second rule rewrites URLs like /xy/abc (with specified language) to /abc.php?lang=xy (language is taken from the first part of the request URL)

Examples:

  • http://mydomain.com/en/references => http://mydomain.com/references.php?lang=en (RewriteRule #2)
  • http://mydomain.com/ro/referinte => http://mydomain.com/referinte.php?lang=ro (RewriteRule #2)
  • http://mydomain.com/references => http://mydomain.com/references.php?lang=en (RewriteRule #1)
  • http://mydomain.com/referinte => http://mydomain.com/referinte.php?lang=en (RewriteRule #1)

Please note that the rules will ignore requests for specific PHP files i.e. requests as follows: http://mydomain.com/ro/referinte.php or http://mydomain.com/referinte.php will not be rewritten. Also, server response should be the same for non-existing pages as currently i.e. assuming /referinte.php does not exist, the requests as follows: http://mydomain.com/en/referinte (new rewriting) and http://mydomain.com/referinte?lang=en (current rewriting) I presume would return 404.

I hope that will help.

于 2013-02-03T03:32:45.783 回答