4

I've created a bunch of RewriteRules for my website and have had no problem with them on my local setup. Here's a snippet from my .htaccess:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^news/?$ news.php [L]

Locally, when I visit 127.0.0.1/news, it redirects properly to news.php and masks the URL. I just updated the .htaccess file on the remote server and receive this error, when trying to visit the above example:

The requested URL /mnt/target02/123456/123456/www.mywebsite.com/web/content/news.php was not found on this server.

I've tried changing the rule to this:

RewriteRule ^news/?$ http://www.mywebsite.com/news.php [L]

and the page loads properly. However, the address bar shows news.php, rather than news. Is there something I am missing, or am I stuck with the ugly (and less secure) address? Thanks!

Nick S.
  • 353
  • 3
  • 13
  • 1
    is that directory `web/content` your document root? – Michael Berkowski Nov 28 '12 at 00:40
  • Yes. web/content is the document root. – Nick S. Nov 28 '12 at 00:43
  • try a slash before news.php like /news.php [L]. Also what makes you think not having a .php extension makes it more secure? if you're trying to hide the fact your server is PHP there are plenty of other ways to discover this.... – WebChemist Nov 28 '12 at 00:55
  • 1
    Wow. That was way too simple. You're right about the security -- I'm more concerned about hiding ugly pages (ie. content.php?id=2012&date=2012-11-25&title=blah). Thanks! – Nick S. Nov 28 '12 at 00:57

2 Answers2

1

If you are like me you are trying to avoid adding leading slashes to your substitution URL (the new destination) so that your same .htaccess file can work with both your test and production servers. I had the same problem you were having and all that was needed was to set:

RewriteBase /

This tells mod_rewrite to base the substitution URLs off of the web root rather than the file root. Another tip, if you need to redirect to the top level of the destination, use ./ instead of just /. My test server has a port (8080) and is not at the root (so it has a non-empty RewriteBase of /site1/), and most of the rewrite rule examples on the web do not consider this case. If you never have your substitutions start with a leading slash, you can use the same .htaccess file on your test server and live server, and all you might need to change is the value of RewriteBase, rather than updating every single rule.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
0

Have you tried adding a leading slash to the destination URL e.g.

RewriteRule ^news/?$ /news.php [L]

arober11
  • 1,969
  • 18
  • 31