Forum Moderators: phranque
I dont like to have different PR values depending on with or w/o www as a part of the URL. I know some people link to me without www but I cant do anything about and thought rewrite rule may be the solution.
I recently put the following code in my httaccess file. Can you tell me if I was correct in doing this, or if there is a better way of doing it.
RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.site-name\.com
RewriteRule (.*) [site-name.com...] [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.site-name\.com
rewriterule (.*) [site-name.com...] [R=permanent,L]
The 302 errors were my biggest problem, and putting the [R=Permanent] code seemed to stop that. A day ago, I put in the [R=301] but I wanted to make sure I was correct in putting both of these in or if there is a different/better way to write this
Be gentle. I'm very new to Apache servers, as well as this site. Although I'm quite knowledgeable, this site is like a gold mine of the minds, it's almost scary. (That's why I've been lurking for about a month) :-)
Thanks,
Ken
For the main Apache config:
<VirtualHost *>
ServerName example.com
Redirect permanent / [example.com...]
</VirtualHost>
<VirtualHost *>
ServerName www.example.com
*** Insert the server config for your actual site ***
</VirtualHost>
Jon
those mod_rewrite setting i have in other .htaccess files in second level directories are supposed to convert dynamic urls into static and look like this:
RewriteEngine on
RewriteBase /books/
RewriteRule ^$ /books3.php?mode=books&node=1000&node2=45&col=4 [NE,L]
RewriteRule images/(.*).html /image.php?ASIN=$1 [NE,L]
RewriteRule (.*).html /book3.php?ASIN=$1&zcat=books [NE,L]
1. any idea why Morgan's mod_rewrite conflicts with the mod_rewrite above?
2. how would Morgan's mod_rewrite be altered to rediret just index.php to www.domain.com if the user is accesssing through domain.com?
If I just copy and paste that code you posted, with the domain name changed of course, into the htaccess that I created after the server move, (to do some successful 301 redirects), will it work or do I need to tweak it somehow?
Sorry for my relative ignorance, man... if I ever get the chance, I'll study this stuff in detail.
The code I posted above is generic. It'll redirect any request with a hostname other than what is specified in the RewriteCond. The hostnames in the first and second lines must match - the first one (in the RewriteCond) with a "!" in front of it (meaning NOT), and properly anchored and escaped as a regular-expressions pattern, the second (in the RewriteRule) as plain-text followed by a back-reference to the requested local pathname ($1).
Some of the code above has anchoring errors -- missing start anchors to be specific. mod_rewrite will run faster if you use start and end anchors where possible (and if needed). Don't end-anchor the hostname in the RewriteCond, though -- it can cause problems if a user-agent appends a port number, as in www.example.com:80/index.html
There's a pretty simple reason we don't want to write code for people in this forum -- We'd rather teach people to fish and feed them for a lifetime, than give them a fish and feed them for only a day. Someone without any understanding of how the code works will be right back tomorrow with another problem or with a request for a new feature. I can't handle it all, even with the generous help of the many other members who contribute here... Not to mention the fact that we all need some time to make a living.
So we ask everyone to read the mod_rewrite documentation and the Apache URL Rewriting Guide, and to post their "best-effort" code. By the time they get to that point, it's usually only a minor point that needs to be improved, or a short discussion to get over some small misunderstanding. Occasionally, I'll break that rule and post some code, especially when the subject is particularly interesting or if it might help a large number of people. Other contributers may do the same, and that's OK, too. But we can't do it too often, or the hungry will quickly overwhelm the fishermen.
So anyway, you might call this a school, but not a free code-writing shop (I charge for that, as do many others here). If you don't have any time to study, try a WebmasterWorld site search on Google for what you want to accomplish, plus the word "RewriteRule" -- that'll usually turn up something. Or post over in the Commercial Exchange forum for bids on the work. I hope that clarifies the issue a bit -- and that is my sole intent here, to clarify; We all have different priorities, and that is understandable.
Here's a free tip for all: Add the following code to the "Custom code insert top" box in your WebmasterWorld control panel:
<center><p> <form method="GET" action="http://www.google.com/search"><input type="hidden" name="domains" value="webmasterworld.com"><input TYPE="text" name="q" size="25" maxlength="255" value><input type="submit" name="btnG" value="Google"><input type="radio" name="sitesearch" value="webmasterworld.com" checked>webmasterworld.com<input type="radio" name="sitesearch" value>www</form></center>
Jim
For the time being, I'll probably assume that the one link without a www won't cause any great damage. I have a massive amount of work to be done wrt the raison d'etre for the site, (enter much data into an .xls, proofread other's notes, begin planning for the next expedition, submit another research application, continue planning for a 7 day transect, through very rough terrain, this Aug in assistance to a bunch of biologists...), and I have to find more funding, (which is why I can't pay for assistance).
Eventually, I will find time to study Apache server mod code. I like a challenge.
Stef
It also looks good in the logs:
"GET / HTTP/1.1" 301
"GET / HTTP/1.1" 200
Thanks, man.