Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite not working in httpd.conf

mod rewrite not working in httpd.conf

         

janet901

8:26 pm on Aug 8, 2005 (gmt 0)

10+ Year Member



I've been trawling through your posts and have found loads of really useful info. Thanks to this I've got quite far with trying to set up a subdomain to subdirectory rewrite.

I have successfully got the following to work in the htaccess file


RewriteEngine on
RewriteCond %{HTTP_HOST}!^(www\.)?example\.com [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com [NC]
RewriteRule .* http://www.example.com/blogs/%1 [R=301,L]

However I want to set up the rewrite to keep the subdomain URL in the address bar

so I have tried using this in the httpd.conf file


RewriteCond %{HTTP_HOST}!^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule ^/(.*) /%1/$1 [L]

The rewrite is completely ignored and if I set the RewriteBase I get a 'Cannot find server' error

I have tried using a simple rewrite with the following:


Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^/(.*)\.html $1.php [NC]

The rule works in htaccess but not in the httpd.conf file.
I think I'm missing something simple but have not been able to find a solution. I've tried using the full path as well as the / for RewriteBase. I greatly appreciate any help with this one.

[edited by: jdMorgan at 8:38 pm (utc) on Aug. 8, 2005]
[edit reason] Repaired formatting. [/edit]

jdMorgan

8:44 pm on Aug 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The key may be this: In .htaccess, the path to the current directory --the directory where this .htaccess file resides-- is stripped. So, in your top-level directory, RewriteRule would see "index.html" as the local URL-path that it then tries to match against your pattern when the page http://www.example.com/index.html is requested.

However, in httpd.conf, this path info is not stripped, so RewriteRule will see "/index.html" as the pathname when that same URL is requested.

Taking that into account, you'll see that a couple of your rules will end up inserting an extra slash into the path, which might be causing the trouble.

Other than that, remember to restart Apache with every change to httpd.conf, and to flush your browser cache after any change to httpd.conf or .htaccess code.

Jim

janet901

9:07 pm on Aug 8, 2005 (gmt 0)

10+ Year Member


Thanks for the fast reply! I have been restarting server and clearing the cache. Also have tried the full path in the RewriteBase. The problem is that when I add the RewriteBase I get 'Cannot find server' in IE and 'Connection refused' in Firefox.

I'm new to configuring apache server and am trying to patch this together from what I have read. Maybe I have got a basic element wrong. I'm putting the mod_rewrite code in the <VirtualHost> tags. Could the problem lie here?

<Virtual Host 000.000.000.0:80>
ServerName example.com
DocumentRoot /full/path/httpdocs
ServerAlias example.com
Options +FollowSymlinks
RewriteEngine on
RewriteBase /full/path/httpdocs
RewriteRule ^/(.*)\.html $1.php [NC]
</VirtualHost>

Thanks again

ChadSEO

9:47 pm on Aug 8, 2005 (gmt 0)

10+ Year Member



Janet,

Couple things. First off, the RewriteBase directive shouldn't be used in a VirtualHost context, and it shouldn't need to be. Secondly, when it is used, you would want to specify the "virtual path", that is, the path as seen on the website, and not the physical path on the server.

The RewriteBase may be used in the .htaccess file, and is often necessary. I believe that's why the code you posted earlier worked in the .htaccess file, and not the httpd.conf file.

The documentation explains things a little more thoroughly, if not quite as consisely, here:
[httpd.apache.org...]

If you take out the RewriteBase in the config you posted for the httpd.conf file, then it should work.

janet901

10:32 pm on Aug 8, 2005 (gmt 0)

10+ Year Member


Ok so I'll stop messing with the RewriteBase. The confusing thing is that

<Virtual Host 000.000.000.0:80>
ServerName example.com
DocumentRoot /full/path/httpdocs
ServerAlias example.com
Options +FollowSymlinks
RewriteEngine on
RewriteBase /full/path/httpdocs
RewriteRule ^/(.*)\.html $1.php [NC]
</VirtualHost>

doesn't work, ie my html file appears. I'm thinking that I must be missing something, but lack of Apache experience is not helping.

Thanks again for the help.

Emma (Janet's my cat!)

ChadSEO

10:46 pm on Aug 8, 2005 (gmt 0)

10+ Year Member



Emma,

Sorry about that, bad habit of guessing people's names :)

I made a few adjustments to what you posted, try this and see if it works any better:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^/([^\.]+)\.html$ /$1.php [NC]

janet901

11:34 pm on Aug 8, 2005 (gmt 0)

10+ Year Member



I'm afraid it's still not working. I've tried a few rewrite examples which work in the .htaccess file and none of them work in the httpd. There must be a wrong setting somewhere...

jdMorgan

11:49 pm on Aug 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



After you get these errors, what does your server error log say?

Jim

janet901

10:41 pm on Aug 9, 2005 (gmt 0)

10+ Year Member



There are no errors in the error log.

This is what I found in the rewrite log
init rewrite engine with requested uri /index.php
applying pattern '^/([^\.]+)\.html$' to uri '/index.php'
pass through /index.php

For some reason the rewrite rules in the httpd.conf file are not being recognised, but they are working in the .htaccess file.

jdMorgan

11:06 pm on Aug 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That shows that the Rule did not match because /index.php was requested, and the Rule only applies to .html files. Is that what you expected?

Jim

janet901

11:26 pm on Aug 9, 2005 (gmt 0)

10+ Year Member



Theres alot of text in there, there's also this
(1) pass through /
(2) init rewrite engine with requested uri /
(3) applying pattern '^/([^\.]+)\.html$' to uri '/'

janet901

10:48 am on Aug 10, 2005 (gmt 0)

10+ Year Member



I'm a novice to apache, so should have been checking errorlog all along. Anyway I'm starting to get some answers as to why my rewrites aren't working.

I tried the following
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^(www\.)?example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule ^/(.*)$ /%1/$1

The error I'm getting is:
File does not exist: /home/httpd/vhosts/default/htdocs/subdomain
The path to my site is /home/httpd/vhosts/example.com/httpdocs. I have Plesk installed and am wondering if this is the reason why the path has been changed.

If I try
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^(www\.)?example\.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule .* home/httpd/vhosts/example.com/httpdocs/%1
I get 'The page cannot found'

and if I try
RewriteEngine on
RewriteCond %{HTTP_HOST}!^(www\.)?example\.com [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com [NC]
RewriteRule .* http://www.example.com/%1 [R=301,L]
I get an external rewrite, which is not what I want as I want to hide the url.

Is there any other way to insert the path. Any help would be massively appreciated as I'm struggling with this.

janet901

12:16 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



Okay I've managed to sort out the file path by tinkering around in the DNS zone file. It's working now for .html and .xml files, but not .php

I've got index.php listed under DirectoryIndex and .php files are readable generaly, but when I try to access them through my mod_rewrite, it tries to download the file. Is there a line I need to throw in to avoid this

<VirtualHost 000.000.000.0>
ServerName example.com
ServerAlias example.com
DocumentRoot /home/httpd/vhosts/example.com/httpdocs
ErrorLog /home/httpd/vhosts/example.com/httpdocs/error_log.log
RewriteLog /home/httpd/vhosts/example.com/httpdocs/rewrite.log
RewriteLogLevel 9
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^(www\.)?example.com [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule^/(.*) /%1/$1 [L]
</VirtualHost>

Nothings coming up in the error log or rewrite log?!

jdMorgan

2:49 pm on Aug 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try adding a PT flag to your rule, making it [PT,L]. This is sometimes necessary to allow other modules to run correctly after mod_rewrite has modified the URL.

Jim

janet901

7:00 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



Thanks, do you have any other ideas as to what I can try. I'm running out of ideas. I've added PT and it hasn't changed. Also I have an Error 403 'Forbidden' to the [sub.example.com...] which comes up as 'Directory index forbidden by rule:' in the error log. I can access [sub.example.com...] and http://www.example.com.

Not sure if this is also connected to the .php problem. .php files are working in www.example.com, just not in the redirect.

I've added
<Directory />
Options Indexes
IndexOptions FancyIndexing
IndexOptions FoldersFirst
</Directory>
to the <VirtualHost> folder but nothing has changed.