PDA

View Full Version : Simple Mod_Rewrite Question


mark87
01-09-2006, 06:34 PM
I need to direct everyone who navigates to /portfolio/ to /portfolio.html - how can I do this?

trib4lmaniac
01-09-2006, 06:38 PM
RewriteRule ^/portfolio/$ /portfolio.html

mark87
01-09-2006, 08:13 PM
Thanks but it doesn't appear to be working...

I placed that in .htaccess in the /portfolio/ directory, I also tried it in the root and it still didn't work. I also added RewriteEngine on at the beginning but still no luck.

Is there anything else I should do?

schleppel
01-09-2006, 10:32 PM
Try it without the first "/" [forward slash].

RewriteEngine On

RewriteRule ^portfolio/?$ /portfolio.html [L]

Put this in root, you also don't need to have the /portfolio/ directory (unless you need it).

Edit: the Apache docs seem to have the first "/" [forward slash], but it never works for me.

mark87
01-10-2006, 12:05 PM
Hmm looked more promising but now I'm getting a 404 error (I've deleted the portfolio directory).

Not Found
The requested URL /portfolio/ was not found on this server.

Additionally, a 301 Moved Permanently error was encountered while trying to use an ErrorDocument to handle the request.

I guess the 301 error is showing the RewriteRule is nearly working?

schleppel
01-10-2006, 03:37 PM
It works fine for me.

The 301 means the ErrorDocument (404 page) sent a 301 permenantly moved header, not mod_rewrite.

The 404 means mod_rewrite didn't work for some reason. Are you sure mod_rewrite is enabled on the server/ for that directory? It doesn't seem to do match the rewriterule, even though it should.

You could try this rewrite rule in the /portfolio/ directory.
RewriteEngine On
RewriteRule ^$ /portfolio.html [L]
although i doubt that'll work if the other one didn't.

mark87
01-10-2006, 03:47 PM
Oo, almost!

I tried the above in the portfolio directory and it's now displaying the page... but with no styling and the links are pointing to the wrong place (they now start with the portfolio directory).

See here (http://www.markaylward.co.uk/portfolio/).

Edit // Ok, decided just to add forward slashes before all links etc so that it still works, that'll do for now. Cheers.