Forum Moderators: phranque
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!
Redirectmatch ^[b]/([^/]*)P[/b]hotos.html$ http://www.DOMAIN.org/photoAlbu[b]m/$1[/b]Photos.html
2) This is just a typo. The first two characters of the pattern are reversed:
RewriteCond %{HTTP_HOST} [b]!^w[/b]ww\.DOMAIN\.org
Flush your browser cache before testing any change to your access-control code.
Jim
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...]
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.DOMAIN\.org
RewriteRule ^(.*)$ http://www.DOMAIN.org/$1 [R=301,L]
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]
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]
#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
Jim
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.
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....