Forum Moderators: phranque
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]
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]
I have tried using a simple rewrite with the following:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^/(.*)\.html $1.php [NC]
[edited by: jdMorgan at 8:38 pm (utc) on Aug. 8, 2005]
[edit reason] Repaired formatting. [/edit]
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
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
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.
<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!)
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.
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.
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?!
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.