RewriteRule Redirects paths not working - apache

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.

Related

I need a 301 htaccess redirect that will find files regardless of case

We are moving to apache from IIS. IIS did not care about case, it served pages whether it was the user capitalized some words or not. But now I'm getting 404 errors all over the place. I need a 301 redirect that will automatically search for similar pages.
For example:
NewHoMepage.htm
will redirect to
newhomepage.htm
and
News25/newnews.htm
will redirect to
news25/NewNews.htm
Our site has 25 directories with 13,000+ pages, so a pages by page redirect is out of the question.
Any help would be appreciated.
You can try this short rule in your htaccess file
RewriteEngine on
RewriteRule ^(.*)$ http://example.com/$1 [R=301,NC,L]
Because each case is different, one cannot simply redirect to lower case.
As you have mentioned you don't want to do redirects for 13k pages, RewriteMap is also out of the question.
The general solution is to use CheckSpelling on in your .htaccess file. To be more specific, you could use CheckCaseOnly on instead.
Here's the documentation for mod_spelling:
http://httpd.apache.org/docs/current/mod/mod_speling.html
Found some interesting solutions here: http://www.askapache.com/htaccess/rewrite-uppercase-lowercase.html.
Personally, I'd go with the second solution, by adding the following in the .conf file:
RewriteEngine on
RewriteBase /
RewriteMap lowercase int:tolower
RewriteCond $1 [A-Z]
RewriteRule ^/?(.*)$ /${lowercase:$1} [R=301,L]
As a side note, R=301 helps you from a SEO perspective, as search engines will update the links for your side and your pages will not be marked as duplicate content.

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.

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

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]

.htaccess redirect doesn't hide url

my .htaccess in the root folder includes the following lines :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ http://example.com/?city=$1 [NC]
when I open the address http://example.com/bla.htm, my browser doesn't hide the GET values specified in the .htaccess, it redirects me to ?city=bla. eventhough I'm not using the [R] switch. This has always worked for me before (as I remember, haven't dealt with htaccess in a while). What's wrong here ?
When you redirect to an entire URL, it doesn't do URL rewriting (you can't exactly rewrite URLs on someone else's website).
Assuming both URLs are on the same server, you need to do something like
RewriteRule ^(.*)\.htm$ index.php?city=$1 [NC]
Also, I'd recommend getting into the habit of using the [L] switch whenever you can - it helps avoid bugs when you have a lot of URLs to rewrite.