Michael
Michael

Reputation: 307

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.

Upvotes: 1

Views: 507

Answers (3)

Paolo Stefan
Paolo Stefan

Reputation: 10263

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.

Upvotes: 0

gen_Eric
gen_Eric

Reputation: 227260

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

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

Upvotes: 3

MarkD
MarkD

Reputation: 4954

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

Upvotes: 1

Related Questions