PDA

View Full Version : Setting up RewriteRules in httpd.conf



sbearman
03-31-2008, 11:49 PM
I want to establish some rewrite directives that will capture all requests for web pages. Since these will not be directory-specific rules, I assume the best place to put them is in my httpd.conf file.

I have set up what I believe to be the correct lines in the file, along with a number of attempts at test rules so I can see if everything is working. Nothing I have tried has led to a successful rewrite, and nothing has appeared in the rewrite log (although the empty log file has appeared).

Here is what I have appended to the end of httpd.conf (the rules are just for testing purposes and to try to get at least a log entry):


<IfModule mod_rewrite.c>
RewriteLog "/etc/httpd/conf/rewrite.log"
RewriteLogLevel 9
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^/rewritetest /existingdirectory/existingpage.php
RewriteRule ^(.*)$ www.mydomain.com/$1
</IfModule>
Earlier in the file are the lines:


LoadModule rewrite_module modules/mod_rewrite.so
and


<Directory "/www/htdocs/">
AllowOverride All
</Directory>
After each edit, I am restarting my VPS.

I assume I have missed a step somewhere along the way and that mod_rewrite is somehow either not turned on or being blocked. Do you know what's missing? Thanks!

wildjokerdesign
04-01-2008, 07:43 AM
Mod Rewrite is available and turned on by default on a WH account. Since you used the phrase "After each edit, I am restarting my VPS." I assume that you are a WH client and that this is an account under them. :)

Try placing your rewrite in Directory tags. There is no need to run this rule unless they are in the public www directory and that may be the problem.
EXAMPLE:


<Directory "/var/www/html">
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/rewritetest /existingdirectory/existingpage.php
RewriteCond %{HTTP_HOST} ^yourdomain\.com
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L]
</Directory>

Note that I added to the rule. You only want the www added if the user did not supply it orginally. :)

sbearman
04-01-2008, 10:25 AM
Thanks for responding so quickly. Yes, I'm a Westhost client. I assumed that this forum was only for Westhost folks.

Adding the directory tags broke the website. I got rid of the second rule in case that was the problem, but it wasn't. I couldn't access the site at all with the directory block in place.

Thanks for the modification of my second rule. My intention was not to add a "www" but just to have a rule that would catch everything. I was hoping to at least get something to show up in the rewrite log that might provide clues as to why my rules are not being applied.

Any other guesses?

wildjokerdesign
04-01-2008, 05:01 PM
Sorry the Directory tags "broke" the site. I did not think about it when I posted but you already have the Directory tags I posted in your httpd.conf and that could have been the problem.

Try adding only the RewriteLog and RewriteLogLevel in your httpd.conf. Then in a .htaccess in /var/www/html add your rules.

sbearman
04-01-2008, 05:58 PM
Well, the rules get applied and work fine in an .htaccess file at the web root directory (though I needed to remove the initial slash - the pattern to look for is "^rewritetest" to capture http://www.mysite.com/rewritetest).

This seems to be a workable solution, though I am still curious as to why the directives are not followed in the .conf file.

Thanks, Shawn, for your generosity in helping me figure this out. I will remember my charity of choice!

wildjokerdesign
04-02-2008, 06:12 AM
Well I still think it involves problems with paths when you insert it into the httpd.conf file. That is the reason I wanted you to place it inside the Directory tags. The problem when you did add the tags may have been that the existing .httpd.conf already has a set of those tags but I had never read that you could not duplicate them. If you want to still give it a try in httpd.conf file then you could try to add your code to the existing Directory tags which should be around lines 290 - 320. You'll notice in those tags that Options FollowSymLinks is already set so you don't need that again.

sbearman
04-02-2008, 05:39 PM
You nailed it, as usual. I did need to put the directives in directory tags.

I also don't think it's a problem to duplicate directory tags. I suspect that the conflict may have occurred because of the duplication of options declarations in the duplicate sets of tags.

Everything is now working as I was hoping it would! Thanks. :D

wildjokerdesign
04-03-2008, 06:59 AM
Great! I am glad it worked out for you. Sorry about the bad guidance at first. If I had actually been doing this myself I would have automatically used the existing tags to add the directives but did not even think to mention that when I suggested it to you. Sometimes my fingers type faster then my brain can keep up. :)

SaintMail
06-10-2008, 11:36 AM
Does anyone know how to use the Rewrite rules to change the case of the domain?

I want to change myexample.com to MyExample.Com

dattas
06-12-2008, 01:50 AM
Forcing a change on a domain name is not possible. All domains out there are all lower case, that is how dns is setup. If you go to microsoft, google, and any other big names, you will see that their domain names do not have any captials in them, that is because the state of the internet right now does not allow it. If you need captials, I would recommend dashes instead, so instead of MyExample.com you would have my-example.com.

wildjokerdesign
06-12-2008, 06:43 AM
If it is purely a marketing issue of your site you can use MyExample.com to market your site. If someone types it in that way or links to it in that method they well still end up at your site. :)

maida
08-26-2008, 02:07 PM
Sorry to barge in on this, but I am needing to do a redirect, and am not sure of how to go about it. I'm a total noob when it comes to the URL rewriting.

I used to have a wordpress installation in the /var/www/html/news directory.

I moved it to the root directory /var/www/html

How can I redirect any calls to a page in the /news/ directory back to the corresponding page in the root?

So if they try to go to
http://www.mydomain.com/news/?page_id=166

I want them redirected to
www.mydomain.com/?page_id=166

Will this do? Or am I missing the point altogether?


<Directory "/var/www/html">
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/rewritetest /news/*
RewriteCond %{HTTP_HOST} ^yourdomain\.com
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L]
</Directory>

sbearman
08-26-2008, 02:53 PM
Maida, the RewriteRule you need is simple. All you should need is the following.


RewriteRule ^news/?(.*) $1

This captures any URI that, immediately after the domain name, starts with "news" and an optional slash, with any number of characters after it. The replacement is just everything except for the news subdirectory.

You should know right away whether the rule is working. Just restart your account and try typing in the URI the way you expect your users to. Let us know whether it works.

maida
08-26-2008, 04:20 PM
Thanks so much for your reply.

Now, do I put this line in my httpd.conf or in an .htaccess file in the news directory?

sbearman
08-26-2008, 04:33 PM
Put everything you wrote in your first post in httpd.conf, but with the rule I suggested:


<Directory "/var/www/html">
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^news/?(.*) $1
</Directory>

You'll have to restart your account for changes in httpd.conf to take effect.

maida
08-26-2008, 04:34 PM
Well, I inserted the bolded line to my httpd.conf file within the directory tags


<Directory />
Options FollowSymLinks
AllowOverride None
RewriteRule ^news/?(.*) $1
</Directory>

And then I restarted my account.

But now I'm getting a 404 error when I try to access any files through a /news/ directory

Not Found
The requested URL /news/ was not found on this server.

Here's the live server: http://www.rockymtnashrae.com/?page_id=166

Should I put a different rule in the .htaccess file of the news directory instead?

maida
08-26-2008, 04:38 PM
Looks like we were posting at the same time.

:doh: I forgot to turn the Rewrite rule on!

It's working now.

Now how can I tweak it so that it actually changes the URL, too?

sbearman
08-26-2008, 04:54 PM
Ah, so you want to redirect! The way to do that with mod_rewrite is to use the [R] flag, but you'll also need the full URI:


RewriteRule ^news/?(.*) http://www.yourdomain.com/$1 [R]

In theory, you don't need mod_rewrite to do this job. You could use the simpler (and less server-intensive) mod_redirect instead. I've never used it, however, so I can't advise.