.htaccess mod_rewrite 301 redirect with nested exceptions? - apache

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]

Related

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/.

RewriteRule Redirects paths not working

I moved my website to a new server with a new CMS so I had to make a lot of 301 Redirects. 'Normal' 301 redirects didn't recognize the url path of my old urls so I tried to make RewriteRules, this is what it looks like now:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^Category http://www.example.com/category [R=301,L]
RewriteRule ^Category/Subcategory http://www.example.com/category-subcategory [R=301,L]
The first RewriteRule works, but as soon as there is a second path in the old url (the second example) the redirect will point to the main cateagy and not the subcategory. So it's basically ignoring the url paths...
Try to invert your rules, or to add a $ at the end of the first one :
RewriteRule ^Category$ http://www.example.com/category [R=301,L]
RewriteRule ^Category/Subcategory http://www.example.com/category-subcategory [R=301,L]
Explanation : Category/Subcategory is also matching the first rule, and as you have use a L flag in the first one, Apache will just use this first rule and don't bother to look further.
For general purpose solution, quoting from apache rewrite guide:
Move Homedirs to Different Webserver Description:
Many webmasters have asked for a solution to the following situation:
They wanted to redirect just all homedirs on a webserver to another webserver. They usually need such things when establishing a newer webserver which will replace the old one over time.
Solution:
The solution is trivial with mod_rewrite. On the old webserver we just
redirect all /~user/anypath URLs to http://example.com/~user/anypath.
RewriteEngine on
RewriteRule ^/~(.+) http://example.com/~$1 [R,L]
In your case URL structure has changed so ôkio's suggestion would work.

Making sure my rewrite rule is a 301 re-direct

I have the following re-write rule which directs krmmalik.com to krmmalik.com/me
How do i make sure this rule is a 301 re-direct, and if it isnt one already, how can i turn it into one?
I've tried using the mixing and matching the tips from this site
http://www.webweaver.nu/html-tips/web-redirection.shtml
as well as Google's Support Articles and existing SO questions, but not having much luck. Note the re-write rule in itself so far has been working fine.
I've also added a CNAME for "www" to "krmmalik.com" in my DNS file. Is that good enough, or do i need to add a specific 301 redirect for that as well?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?krmmalik.com$
RewriteRule ^(/)?$ me [L]
Try the following:
RewriteRule ^/?$ /me [L,R=permanent]
The R=permanent flag instructs a 301 status redirect (and you can use R=301 if you prefer, but I think that "permanent" is more readable).
Putting a forward-slash at the start of the /me target URL will tell Apache to redirect the user to the directory named "me" at the web server's public root directory. So in your case it should redirect the user to krmmalik.com/me (or www.krmmalik.com/me).
Also, you don't need to wrap the match pattern in parentheses, because you don't need to capture the slash for later use. So ^/?$ will do the job fine.

301 redirect query string to SEO friendly URLs through .htaccess

I’ve written some code on my .htaccess file which allows the use of SEO friendly URLs instead of ugly query strings. The following code rewrites the SEO friendly version in the browser to the query string version on the server.
RewriteEngine On
RewriteRule ^seo/([^/]*)/$ /directory/script.php?size=large&colour=green&pattern=$1 [L]
So that the ugly
http://www.mysite.com/directory/script.php?size=large&colour=green&pattern=striped
Is now beautiful
http://www.mysite.com/directory/seo/striped/
Just to explain the code a bit; seo is there to add more keywords to the URL, /directory/ is the directory in which the .htaccess file is located, parameters size=large and colour=green never change, while pattern=$1 can be many different values.
The above code works perfectly. However, the problem is I am now stuck with two URLs that point to exactly the same content. To solve this, I would like to 301 redirect the old, ugly querystrings to the SEO friendly URLs. What I have tried so far does not work - and Google is not being particularly friendly today.
Can anybody offer working code to put in my .htaccess file that redirects ugly to new URL, while retaining the rewrite? Thanks!
This should do the trick:
RewriteEngine On
## Redirect to pretty urls
# The '%1' in the rewrite comes from the group in the previous RewriteCond
RewriteCond %{REQUEST_URI} !seo
RewriteCond %{QUERY_STRING} ^size=large&colour=green&pattern=([a-zA-Z]*)$
RewriteRule (.*) /directory\/seo\/%1\/? [L,R=301]
## Rewrite to long url, additional parameter at the end will cause
## the internal redirect not to match the previous rule (would cause redirect loop)
RewriteRule ^directory\/seo\/([^/]*)/$ /directory/script.php? size=large&colour=green&pattern=$1&rewrite [L]
You can also match the size and colour if needed, by changing those to regex groups as well, and using the corresponding %N
Hope this helps.
Not tested, but this may work...
RewriteRule ^directory/script.php?size=large&colour=green&pattern=(.*)$ /seo/$1/? [R=301,NE,NC,L]

Replace one URL with another by using mod_rewrite

Current situation
RewriteRule ^$ /index.php?page=Portal [R=301,L]
When a user comes to the website and goes to the "root" url of the domain (RegExp "^$") he's redirected to /index.php?page=Portal
That's working.
Now we have "index.php?page=Portal" in the google index and we have tons of links to that page on various locations all over the internet.
Intended new situation
We want the portal page to show up on the root url - no redirect. That's no problem... Just remove the redirect:
RewriteRule ^$ /index.php?page=Portal [L]
Now we also want the old url to redirect to the new location, and that's where I fail but can't see why:
RewriteCond %{QUERY_STRING} ^page=Portal$
RewriteRule ^index.php$ http://www.jacatu.de/? [R=301,L]
As soon as I do this I end up in a redirect loop:
(When I change to 302 in .htaccess I see 302 redirects, so the loop really seems to be caused by mod_rewrite)
But why? All rules are marked as last [L] - so I think I can rule out that rule 2 triggers rule 1.
I enabled logging as suggested by Jacek Prucia and in fact it looked like having [L] in the URL doesn't stop execution. Both rules were processed.
I now changed the first rewrite to
RewriteRule ^$ /index.php?page=Portal&int=1 [L]
so that it doesn't match the RewriteCond of the internal rewrite so theoretically my problem is solved. It would be nice to know, though, why it did what it did. :)