2

I'm trying to write a RewriteRule to make a simple url. I want users to be able to type enter

www.example.com/somepage

and have it take them to

www.example.com/abc/somepage.php

How can this be done in .htaccess?

I've tried these to no avail:

RewriteRule ^somepage$ abc/somepage.php [L]

RewriteRule ^/somepage$ /abc/somepage.php [L]

Your help is much appreciated.

1
  • As it turns out, this isn't a simple duplicate of the "Apache htaccess redirects" question, linked to above, since the answer is dependent on the nginx server.
    – MrWhite
    Commented May 5, 2013 at 10:59

1 Answer 1

1

Assuming this is in the .htaccess in your document root, then you are almost there. You just seem to be missing the / (slash - directory separator) from the front of your destination URL (in the first example).

RewriteEngine On
RewriteRule ^somepage$ /abc/somepage.php [R=301,L]

The slash (or whatever is the base URL) is automatically removed from the request URI when pattern matching, which is why it is not present in the pattern (^somepage$).

This will perform an external redirect (R=301) - so the destination URL will show in the browser. Remove the R flag to perform an internal rewrite (URL won't change).

13
  • Thanks for the response. This isn't working for me.. I have no idea why. And sorry, I meant to only have [L], so the dest url will not show in the user's browser. Commented May 1, 2013 at 18:46
  • This is the literal code: RewriteRule ^hatfinder$ /gbi/hatfinder.php [L] You can see it not working here: www.goorin.com/hatfinder Commented May 1, 2013 at 18:48
  • In what way "not working"? Does anything happen? Is mod_rewrite enabled? Is FollowSymLinks enabled?
    – MrWhite
    Commented May 1, 2013 at 18:49
  • Yes, they are both enabled. I'm getting 404 Commented May 1, 2013 at 18:51
  • 1
    So I figured it out.. the rewrites are handled in the .conf files for the nginx server. They can be done in .htaccess, but the .conf doesn't let it get that far it seems.. (not sure why we still have a .htaccess file), and you're right, in the .conf file it is written rewrite Commented May 2, 2013 at 15:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.