views:

441

answers:

5

Hello to everybody! I'm developing a web site containing 3 pages (Home, page2, page3) ... in the second page there is a navigation bar, with 4 items (subpage1, subpage2, ...), that I use to replace the content of the page 2 with url variables! In other words, the second item of the navigation bar in page2 points to:

http://localhost/uk/page2/index.php?pg=subpage2

the item 3 point to:

http://localhost/uk/page2/index.php?pg=subpage3

Now I would like to use more friendly urls via .htaccess!

I've written this:

RewriteEngine On
RewriteRule /uk/page2/(.*)/$ /uk/page2/index.php?pg=$1

in the .htaccess placed in the root!

But doesn't work! Please help!!!

+3  A: 

When you're using .htaccess you don't have the leading slash:

RewriteEngine On
RewriteRule ^uk/page2/(.*)/$ /uk/page2/index.php?pg=$1
Greg
Thanks to your answer!Also with this, the rewrite doesn't work!!!
When you say it doesn't work, what happens exactly?
Greg
Try just putting some rubbish in the .htaccess - you should get 500 Server Error. If you don't, your .htaccess isn't being loaded
Greg
The url doesn't change! It's anytime in dynamic format!
@coder: mod_rewrite can only rewrite the URL you are requesting. So you need to request `/uk/page2/subpage2/` to see if the rewrite works.
Gumbo
OK! Thanks ... I'm sorry but I'm beginner in apache and php!
A: 

It may be the trailing slash at the end, so change this:

RewriteEngine On
RewriteRule /uk/page2/(.*)/$ /uk/page2/index.php?pg=$1

to this:

RewriteEngine On
RewriteRule ^(.*)uk/page2(/?)(.*)$ /uk/page2/index.php?pg=$3

Another thing you should check is that you have AllowOverride set to All in your httpd.conf file, instead of None. If it is set to None, you won't be allowed to do anything with .htaccess.

Adam Plumb
A: 

G'day,

I'd suggest enabling the RewriteLog config option at a high level to check what's actually happening under the covers.

Has AllowOverides been enabled?</obvious> (-:

Seems like you're out of luck using .htaccess

Unbelievably mod_rewrite provides URL manipulations in per-directory context, i.e., within .htaccess files, although these are reached a very long time after the URLs have been translated to filenames. It has to be this way because .htaccess files live in the filesystem, so processing has already reached this stage. In other words: According to the API phases at this time it is too late for any URL manipulations. - Apache mod_rewrite doc.s (emphasis mine)

Rob Wells
A: 

Thanks to everybody! Now all works fine!

I've notice than if I request:

http://localhost/uk/page2/subpage5/

the browser shows me the page2 even if

http://localhost/uk/page2/index.php?pg=subpage5

doesn't exist! How could I avoid this behavior? I must write a condition in the index.php?

yes .
SilentGhost
Your script should know best which pages exist and which don’t. So you should do a validation, check if the request is valid and return either the requested page or an error page with the right status code (`404 Not Found`).
Gumbo
A: 

But now that I've introduced this rewriterule, in my sitemap I must write the dynamics urls or the statics ones?