开发者

Can I use PHP and/or htaccess to rewrite a URL but not redirect?

Is there a way I can use PHP (and/or .htaccess) to rewrite the URL of a page.

For example if a user goes to www.mysite.com/french they are actually accessing 开发者_运维知识库the page which is www.mysite.com/index.php?lang=fr

But not a redirect.


You want to use mod_rewrite and an .htaccess file to achieve this.

RewriteEngine On
RewriteBase /
RewriteRule ^french/(.*)$ /index.php?lang=fr [L,QSA]


Yes, using Apache mod_rewrite and appropriate rules in an .htaccess file.

The docs on mod_rewrite are here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html


on the Apache site you can find several examples of URL rewriting flavors, by the way it's enough to use something like this in an .htaccess file:

RewriteEngine on
RewriteRule ^french(/|)$ /index.php?lang=fr  [flag]

Where [flag] can be one of the following:

[PT]
[L,PT]
[QSA]
[L,QSA]

You may want to have a look at the PT (passthrough) flag docs or RewriteRule flags docs.

Also, pay attention to what your links are pointing to: in fact, the RewriteRule first argument is a regular expression that will be used to match the URLs to be rewritten. At the moment,

^french(/|)$

matches "french", right after the domain name,followed either by a slash (/) or nothing (that's the meaning of (/|) ); that is, it'll match www.mysite.com/french and www.mysite.com/french/ but nothing else. If you need to parse query string arguments, or subpaths, then you may need a more complex regex.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜