Forum Moderators: phranque

Message Too Old, No Replies

problem getting .htaccess rewrite and redirectmaatch to work

for (a) redirecting to new directory and (b) rewriting domain name

         

zollerwagner

9:47 am on Feb 5, 2005 (gmt 0)

10+ Year Member



I have two questions about .htacess issues that I've spent hours trying to figure out....

1.) I have changed a site's directory structure so that a few files that used to be in the site root are now in a new directory. The files all end with "Photos.html".

Having tried many variations, I was unable to get RedirectMatch to work:

redirectmatch ^(.*)Photos.html$ [DOMAIN.org...]

This would cycle over and over adding another /photoAlbum/ directory to the URL until the process timed out.

Is there a simple solution to this?

Do I need to use [301] with RedirectMatch?

I was able to get the functionality I need with with rewrite by adding the RewriteCond (which stopped the cycling):

RewriteCond %{REQUEST_URI}!^/photoAlbum/*
RewriteRule (.*Photos.html) [DOMAIN.org...] [R=301,L]

Is there any advantage to using RedirectMatch?

2.) I have two domain names (same name one with .org and the other with .com). The .com version is parked and pointing to the same site. I'd like to be sure that I'm not indexed in the search engines under the .com version. So I copied some code JLMorgan suggested (exactly, I think):

#redirect anyone using a shorthand URL or .com domain name an external permanent redirect to the correct canonical URL
RewriteCond %{HTTP_HOST} ^!www.DOMAIN.org
RewriteRule ^(.*)$ [DOMAIN.org...] [L,R=permanent]

It doesn't work. If I browse to the the .com address and move around the site, it still displays the .com in the address bar.

Does having the .com version parked on the server prevent this rewrite from working?

TIA!

jdMorgan

4:27 pm on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) A simple problem of using an ambiguous regular-expression. Change it to accept any number of characters that are NOT a slash:

Redirectmatch ^[b]/([^/]*)P[/b]hotos.html$ http://www.DOMAIN.org/photoAlbu[b]m/$1[/b]Photos.html

This will cause the pattern to reject any URL with a subdirectory already added.

2) This is just a typo. The first two characters of the pattern are reversed:


RewriteCond %{HTTP_HOST} [b]!^w[/b]ww\.DOMAIN\.org

(Also escape the literal periods.)

Flush your browser cache before testing any change to your access-control code.

Jim

zollerwagner

6:20 pm on Feb 5, 2005 (gmt 0)

10+ Year Member



As I woke up this morning, I realized that I had a typo in your user name, Jim. Sorry about that!

Following your last message, I tried this for the rewrite of domain name:

RewriteCond %{HTTP_HOST} (SPACE)!^www\.DOMAIN\.org
RewriteRule ^(.*)$ [DOMAIN.org...] [L,R=permanent]

*(SPACE) I had to add something to show the space because the forum removed the space in the RewriteCond

The server gave me an alert (redirection limit exceeded) after it seemed to keep looping. Interestingly, the log shows that a 301 resulted and that it served up the /index.html. (I didn't see it!)

The RedirectMatch now works perfectly. Thanks! I would have worked on that for ages without thinking of your simple solution. I added 301 to it like:

redirectmatch 301 ^/([^/]*)Photos.html$ [DOMAIN.org...]

jdMorgan

8:02 pm on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll need to add a check to that code to make sure it doesn't try to redirect forever if HTTP_HOST is blank. This can happen if you are behind an HTTP/1.0 prox. HTTP/1.0 does not support the HTTP_HOST header, and the code can't/won't work for HTTP/1.0. This is usually only a minor annoyance.

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.DOMAIN\.org
RewriteRule ^(.*)$ http://www.DOMAIN.org/$1 [R=301,L]

I use a bold-unbold tag pair before the "!" to stop the forum from deleting spaces, BTW. :)

Jim

zollerwagner

9:22 pm on Feb 5, 2005 (gmt 0)

10+ Year Member



Oh, boy, I'm sorry to keep bugging you , Jim.

I found one problem. I had placed these rewrite lines above the RewriteEngine On directive. That's fixed.

I'm still stumped. I can't get into the site, no matter which of these I try:
www.DOMAIN.org
DOMAIN.org
DOMAIN.com

I get the same error message and it doesn't show me the index page. I'm not sure, but I don't think it's appearing in the log files, either.

I must have some error in the rest of the .htaccess file. Here's everything in it:


# on error send to default doc
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 410 /410.html

# allow ssi with .html etal
AddType text/html .shtml .shtm .htm .html
AddHandler server-parsed .shtml .shtm .htm .html
Options +FollowSymLinks +Includes -Indexes

RewriteEngine on

#permanent redirect of old pages to new
http://www.DOMAIN.org/photoAlbum/$1 [R=301,L]
redirectmatch 301 ^/([^/]*)Photos.html$ http://www.DOMAIN.org/photoAlbum/$1Photos.html

#redirect anyone using a shorthand URL or .com domain name an external permanent redirect to the correct canonical URL
#if HTTP_HOST is NOT blank and is NOT www.DOMAIN.org, redirect
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.DOMAIN\.org
RewriteRule ^(.*)$ http://www.DOMAIN.org/$1 [R=301,L]

# COULD REMOVE THE OPTIONAL www. CODE ONCE DOMAIN REWRITE WORKS

# Block image HotLinks from outside our domain except Google, AltaVista, Gigablast, Comet Systems, and SearchHippo translators and caches
RewriteCond %{HTTP_REFERER}!^$
RewriteCond %{HTTP_REFERER}!IPADDRESS/~USERNAME [NC]
RewriteCond %{HTTP_REFERER}

!^http://(www\.)?DOMAIN\.org [NC]

# for some versions of Netscape, must add a condition to allow images on document.write-created pages
# (wysiwyg://[0-9]*/)? for netscape 4.75 on PC (Mac?)
# (wyciwyg://[0-9]*/)? for Netscape 7 (others?) on Mac
RewriteCond %{HTTP_REFERER}!^(wy[sc]iwyg://[0-9]*/)?http://(www\.)?DOMAIN\.org [NC]

RewriteCond %{HTTP_REFERER}!^http://216\.239\.(3[2-9]¦[45][0-9]¦6[0-3]).*(www\.)?DOMAIN\.org [NC]
RewriteCond %{HTTP_REFERER}!^http://babel.altavista.com/.*(www\.)?DOMAIN\.org [NC]
RewriteCond %{HTTP_REFERER}!^http://216\.243\.113\.1/cgi/
RewriteCond %{HTTP_REFERER}!^http://search.*\.cometsystems\.com/search.*(www\.)?DOMAIN\.org [NC]
RewriteCond %{HTTP_REFERER}!^http://.*searchhippo\.com.*(www\.)?DOMAIN\.org [NC]

RewriteRule \.(gif¦jpg¦jpeg?¦png)$ - [NC,F]

# ban LWP bot from accessing files
RewriteCond %{HTTP_USER_AGENT} ^lwp* [NC]

# F = forbid, L = last item in rewrite engine for this condition, but redundant with F and G
RewriteRule .* - [F]

jdMorgan

10:04 pm on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I get the same error message and it doesn't show me the index page

What error message?

What does your server error log show?

Jim

jdMorgan

10:14 pm on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, assuming you're talking about a redirection loop...

Let's replace the RedirectMatch with a RewriteRule, and make it much more specific:


#permanent redirect of old pages to new
RewriteCond %{REQUEST_URI} !^/photoAlbum/.+Photos\.html$
RewriteRule ([^/]+)Photos\.html$ http://www.DOMAIN.org/photoAlbum/$1Photos.html [R=301,L]

Also, there are several extra newlines in your code, possibly a result of copy-and-paste to this forum. But these newlines must be removed. Example:

#permanent redirect of old pages to new
[b]http:[i][/i]//www.DOMAIN.org/photoAlbum/$1 [R=301,L][/b]
redirectmatch 301 ^/([^/]*)Photos.html$ http://www.DOMAIN.org/photoAlbum/$1Photos.html

That newline before the bolded text will generate a parsing error.

Jim

zollerwagner

2:13 am on Feb 6, 2005 (gmt 0)

10+ Year Member



Oops. You're right. The cut and paste error was into the forum, not in the .htaccess file.

Sorry for the confusion.

The lines in my .htaccess file really say:


#RewriteCond %{REQUEST_URI}!^/photoAlbum/*
#RewriteRule (.*Photos.html) http://www.DOMAIN.org/photoAlbum/$1 [R=301,L]

The photoAlbum code works fine, the problem is with what I would expect to be simpler: redirecting to www.DOMAIN.org from DOMAIN.org or (www.)?DOMAIN.com.

The status bar of the browser (Firefox) shows that there are many attempts to retrieve the file, but no record appears in the logs.

This is different than the earlier cycling because then the name of the file appeared in the log file, and each time it did another layer of that directory was added to the requested file name, like:
/photoAlbum/photoAlbum/photoAlbum/photoAlbum/photoAlbum/index.html

The error message is a browser alert:
Redirection limit for the URL exceeded . Unable to load the requested page. This may be caused by cookies that are blocked.

Interestingly, this last time I did get the index page--without getting an alert, and the domain was changed to www.DOMAIN.org reliably. But the index was didn't successfully load the SSI include files, and I couldn't move to another page from that one without getting the alert message. This time the log file showed the index ( / ) and the other page both as 301.

I'll take another look at the top of the .htaccess.

zollerwagner

4:47 am on Feb 6, 2005 (gmt 0)

10+ Year Member



I figured it out. Phew, at last!

In the URL, I had used some capital letters. When I changed them all to lowercase, the server loved it.


#redirect anyone using a shorthand URL or .com domain name an external permanent redirect to the correct canonical URL
#if HTTP_HOST is NOT blank and is NOT www.domain.org, redirect
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.domain\.org
RewriteRule ^(.*)$ http://www.domain.org/$1 [R=301,L]

For some reason, "myDomain.org", for example won't work, but "mydomain.org" will.

Thanks, Jim! You were very helpful. Now I'm off to clean up the file....