1

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

1 Answer 1

2

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 ([^\/]*)

Sign up to request clarification or add additional context in comments.

3 Comments

example.com/en/blog.html to example.com/index.php?p=blog&pid=2&lid=1&l=en. -- i think it's the other way around. but I do get your idea.
I see. Are you trying to rewrite or redirect the URLs? I'd understand the latter as a way of not breaking legacy links to your existing site, while trying to upgrade the URI design. If that's the case, then you can safely use the above, as the original links (.../index.php?p=blog&...) will still work.
I think you are right, I have rewrite my URLs using PHP to make it flexible. thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.