htaccess file ignoring www redirect - apache

I finally got around to using htaccess to properly redirect my www pages to non-www urls, but I can't seem to get it to work in any way I've tried. I believe something that was included in the htaccess before I changed it is causing the problem.
I added
#www redirect
RewriteCond %{HTTP_HOST} www\.(.+)$
RewriteRule (.*) http://%1/$1 [R=301]
in this http://pastebin.com/vAQ45Mky, but it seems to ignore it completely. My night spent searching for answers has turned up nothing useful, so thanks for any help you can give!

That's a big file, and I don't know what you've tested, but the first thing I'd try would be adding an L flag:
RewriteRule (.*) http://%1/$1 [L,R=301]
Without that flag, it'll keep trying the other rules, and apply those that hit. At which point I'm not sure if it is even defined what happens when first applying an absolute-URL rewrite, then (one or more) path rewrites, but I would not be surprised to see the hostname part of the first rewrite lost.

Related

Htaccess 301 redirect with variables

I am having some problems. I switched over from one e-commerce system to another a while back and this changed the url structure of some of the pages. I was fine with this and set up some rewrite rules. Somehow this has stopped working and I am not sure why.
I need to change
/product/{Category-Name}/{URL} to /{URL}
/ecategory/{Category-ID}/{URL} to /{URL}
/manu/{URL} to /{URL}
/news/{category-name}/{URL} to /blog/{URL}
I am using
RewriteRule ^/?(product|ecategory)/([^\/]+)/(.+?)\$ /$3 [R=301,L]
RewriteRule ^/?(manu)/(.+?)\$ /$2 [R=301,L]
RewriteRule ^/?(news)/([^\/]+)/(.+?)\$ /blog/$2/$3 [R=301,L]
RewriteRule ^/?(newscategory)/([^\/]+)/(.+?)\$ /blog/$3 [R=301,L]
RewriteRule ^/?([0-9]+)/(.+?)\$ /$2 [R=301,L]
Bu this is not working now and I don't have enough knowledge of htaccess redirects to work out what is wrong. I have been making changes and looking online but I feel like I am going in circles and this is very confusing.
Please, can anyone give me some advice?
I solved it by removing the / before the $. This was my mistake from when I think the URLs used to have .html at the end.

301 Redirect not working in Apache

I'm experiencing a frustrating problem with Apache rewrite rules and a 301 redirect.
We have an extensive list of RewriteRules, all of which work properly. Now I want anyone who goes to the root of our site to be redirected to another site. Here are the applicable rewrite rules in Apache:
RewriteRule "^/index.html" "http://newsite.somewhere.com" [R=301,L]
RewriteRule "^/special-programs" "http://newsite.somewhere.com/special-programs/" [R=301,L]
RewriteRule "^/prospective/" "http://newsite.somewhere.com/adms" [R=301,L]
RewriteRule "^/current/careers/" "http://newsite.somewhere.com/careers" [R=301,L]
RewriteRule "^/current/" "http://newsite.somewhere.com/current" [R=301,L]
Each of these redirects work except for the first one, the root of the site. It redirected using the defaults. However, as soon as I add [R=301,L] it simply stops redirecting all-together, while the other redirects continue to work properly. (A 301 redirect is a requirement; we can't use the default 302.)
I will add that there are many (dozens) redirects above these, but none of them involve the root of the site.
I've tried moving the RewriteRule to the top, to the bottom, and a few other places. No change. I'm reluctant to comment out the dozens of Rewrite rules above but will do so if last resort.
The rewrite rules are in a conf file under /etc/httpd/conf.d.
This seems like a straightforward scenario, so it is unclear to me why the 301 redirect isn't working. If anyone can provide some insight, it would be much appreciated. Thank you very much!

301 Redirect Whole Site Except One Folder, leave Add-on domains alone

I have a brainteaser and need help from people smarter than me. I have a shared hosting account. I'd like to 301 forward the root URL (say, domain.org) to a new URL. I also want one folder (/blog/) to be left alone (not forwarded). I was able to find an example of this here, and I put together this potential scenario for doing that:
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ http://newdomain.org/$1 [L,R=301]
I believe that this should be OK, but here's the trick: I have add-on domains in this hosting, and if I use the above, I'm pretty sure that I will forward every one of them to newdomain.org, not just domain.org. I did some testing using more specific text strings in the first spot following RewriteRule, but I can't seem to get the syntax without blowing up my site and getting a 500.
Any ideas would be greatly appreciated!
Thanks, Dave
Try adding another condition:
RewriteCond %{HTTP_HOST} ^domain\.org$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ http://newdomain.org/$1 [L,R=301]
Where domain.org is the domain that you want everything to be redirected to newdomain.org, except /blog/.

.htaccess mod_rewrite 301 redirect with nested exceptions?

I need a little help with my .htaccess before I deploy it!
I want to 301 redirect almost everything from elementalthreads.com to ethreads.com, excluding blog/wp-content/uploads, and /pommo.
Am I doing this right?:
RewriteEngine on
#exclude old uploads folder and /pommo
RewriteCond %{REQUEST_URI} !^/(blog/wp-content/uploads|pommo) [NC]
RewriteRule (.*) http://ethreads.com/$1 [R=301,L]
Will that transfer canonical pagerank?
Here's where I know I need help:
The old site has a wordpress blog, which I've cloned on the new domain. I'd love to preserve the permalinks, which are almost 1:1, eg:
http://www.elementalthreads.com/blog/ethreads-now-on-amazon-com/ redirects to
http://ethreads.com/ethreads-now-on-amazon-com/ (note /blog/ is missing here)
And the blog index http://www.elementalthreads.com/blog/ should redirect to http://ethreads.com/blog/, which seems like an exception to the above rule, since "/blog/" should only be preserved here?
I'm stumped about how to regEx or otherwise define these last two conditions/rules. Any help would be most appreciated!
That looks correct to me. However, you should not put this live without checking it, there really is nothing preventing you from being able to test it. One thing to bare in mind is that browsers can cache 301 response codes so when testing you should use [R,L] as your flags. Once you are happy add the [R=301,L] back in before deployment.
OK for points (1) & (2)
# only redirect the blog direcotry
RewriteRule ^blog/?$ http://ethreads.com/blog/ [NC,R=301,L]
# redirect all sub folders of blog to the new domain
RewriteRule ^blog/([\w-])/?$ http://ethreads.com/$1/ [NC,R=301,L]

Why would mod_rewrite rewrite twice?

I only recently found out about URL rewriting, so I've still got a lot to learn.
While following the Easy Mod Rewrite tutorial, the results of one of their examples is really confusing me.
RewriteBase /
RewriteRule (.*) index.php?page=$1 [QSA,L]
Rewrites /home as /index.php?page=index.php&page=home.
I thought the duplicates might have had been caused by something in my host's configs, but a clean install of XAMPP does the same.
So, does anyone know why this seems to parse twice?
And, to me this seems like, if it's going to do this, it would be an infinite loop -- why does it stop at 2 cycles?
From Example 1 on this page, which is part of the tutorial linked in your question:
Assume you are using a CMS system that rewrites requests for everything to a single index.php script.
RewriteRule ^(.*)$ index.php?PAGE=$1 [L,QSA]
Yet every time you run that, regardless of which file you request, the PAGE variable always contains "index.php".
Why? You will end up doing two rewrites. Firstly, you request test.php. This gets rewritten to index.php?PAGE=test.php. A second request is now made for index.php?PAGE=test.php. This still matches your rewrite pattern, and in turn gets rewritten to index.php?PAGE=index.php.
One solution would be to add a RewriteCond that checks if the file is already "index.php". A better solution that also allows you to keep images and CSS files in the same directory is to use a RewriteCond that checks if the file exists, using -f.
1the link is to the Internet Archive, since the tutorial website appears to be offline
From the Apache Module mod_rewrite documentation:
'last|L' (last rule)
[…] if the RewriteRule generates an internal redirect […] this will reinject the request and will cause processing to be repeated starting from the first RewriteRule.
To prevent this you could either use an additional RewriteCond directive:
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule (.*) index.php?page=$1 [QSA,L]
Or you alter the pattern to not match index.php and use the REQUEST_URI variable, either in the redirect or later in PHP ($_SERVER['REQUEST_URI']).
RewriteRule !^index\.php$ index.php?page=%{REQUEST_URI} [QSA,L]