user299420
user299420

Reputation:

modrewrite php query string

I would like to change:

http://example.com/index.php?p=blog&pid=2&lid=1&l=en

into just

http:// example.com/en/blog.html

or just

http:// example.com/blog.html

Upvotes: 1

Views: 194

Answers (1)

MicE
MicE

Reputation: 5128

I think that you are trying to achieve more friendly URLs, so I'm assuming that wan to internally rewrite http://example.com/en/blog.html to http://example.com/index.php?p=blog&pid=2&lid=1&l=en.

The most practical way that I can recommend is to rewrite everything to your PHP script, and do the rewriting internally in PHP. That way your PHP application can have full control over URIs.

RewriteEngine On
RewriteBase /

RewriteRule .* index.php [L]

In PHP, you can then access the original URI via superglobals: $_SERVER[ "REQUEST_URI" ]. Note: sanitize, sanitize, sanitize!


Edit

If you want to do the whole thing via .htaccess, then see the example below. Note however that if you'll expand your URI structure in the future (i.e. by adding new rules), then this approach might become more difficult to maintain as opposed to doing it within your PHP application.

RewriteEngine On
RewriteBase /

# http://example.com/en/blog.html
RewriteRule ^([^\/]*)/([^\/]*)\.html$ index.php?l=$1&p=$2 [NC,QSA,L]

# http://example.com/blog.html
RewriteRule ^([^\/]*)\.html$ index.php?p=$1 [NC,QSA,L]

# alternatively you can change the last line to add a default language (if not present)
RewriteRule ^([^\/]*)\.html$ index.php?l=sk&p=$1 [NC,QSA,L]

Edit #2: added a missing ^ character in all three rules, i.e. changed ([\/]*) to ([^\/]*)

Upvotes: 2

Related Questions