Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite rules ignored

mod_rewrite rules ignored

         

Leirith

10:57 pm on Dec 13, 2010 (gmt 0)

10+ Year Member



Hi,

I've had success with mod_rewrite before but this week, upon setting up a new server, I seem to be missing something.

I have enabled mod_rewrite and it shows up in phpinfo() under apache2handler.

A simple test however is failing.

In /var/www I have:
  • test.html
  • test.php
  • .htaccess

The .htaccess file includes the following:

RewriteEngine On 
RewriteRule ^test\.html$ test.php [L]


The httpd.conf file includes the following:

<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>


When I go to mydomain.com/test.html, it never shows test.php. test.php works if I browse to it directly though. I haven't got any RewriteRules to work. The only related directive I've seen work lately is the following example from [httpd.apache.org...] :

RedirectMatch ^/$ http://example.com/e/www/


It works when pasted into httpd.conf, but any RewriteRules do not work there either.

Hope someone could lend a hand.

Thanks.

jdMorgan

1:52 am on Dec 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It would clarify things --though likely not change the outcome-- if test.html *did not* exist as a 'real' file.

Assuming that you've got DocumentRoot pointed to the /.htaccess file's directory, then this should work -- It seems you've got the required Options and Overrides set.

BTW, if you've got access to the config files, there's no need to use .htaccess files, and it would be more efficient and a bit safer not to do so.

Jim

Leirith

2:53 am on Dec 14, 2010 (gmt 0)

10+ Year Member



Thanks for the response. I wouldn't normally use .htaccess, but I was running out of ideas, so I ended up trying it.

I have moved the configuration into a virtual host section [/etc/apache2/sites-available/mydomain.com]

<VirtualHost *:80>
ServerName mydomain.com
ServerAdmin webmaster@mydomain.com
DocumentRoot /var/www/mydomain.com/docroot/
ServerSignature Off

<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>

RewriteEngine On
RewriteRule ^oldstuff\.html$ newstuff.html [R]

</VirtualHost>


The simple rewrite rule above is similarly ineffective. I can browse to mydomain.com/newstuff.html, but if I browse to mydomain.com/oldstuff.html, I get a broken link error.

Any thoughts about why this mightn't be working would be much appreciated.

jdMorgan

2:57 pm on Dec 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Note that in .htaccess or in a <Directory> section in a config file, the URL-path examined by RewriteRule is stripped of the current directory prefix. (This behaviour is described in the mod_rewrite documentation.)

If located in a config file outside of any <Directory> container, the URL-path pattern in the RewriteRule must match the entire requested URL-path. So in this case:

RewriteRule ^/oldstuff\.html$ http://www.example.com/newstuff.html [R=301,L]

(The leading slash on the pattern is required, and the canonical URL and explicit flags are highly-recommended)

Jim