UrlRewrite & Redirect with an optional /XX/home part in Apache2 - apache

For a perfect working and Error404-Free log file, I have to redirect all previous (now wrong) home page urls to the new homepage. For that I have a partially working rewrite:
RewriteRule ^home$ /en/aster [R=301]
// Works for site.com/home but NOT for site.com/en/home or site.com/xx/home etc.
Q1. How to make an optional part ?????/ sothat site.com/anything/home permanently redirects to site.com/anything/home making sure that the root is not another folder but the actual real sites root followed by the two language chars then /home?
Q2. for consistency and SEO purposes, Do I need a [R=301] or a [R=301,L] ?
Your ideas and help is highly appreciated.

Not sure if I completely understand what you want the site.com/anything/home to redirect to, but this is how I understood:
RewriteRule (.*)/home $1/en/aster [R=301,L] Will match all requests ending in "/home". The (.*) will keep the urls in the same path. Therefore site.com/anything/home redirects to site.com/anything/en/aster
As far as SEO, there is no difference between [R=301,L] and [R=301]. The L informs mod_rewrite to not process any further rules, which in this case is what you would want, since you are redirecting to a new page.
Hope this helps.

Related

Apache redirect if no parameters

I tried searching for this solution and apologize ahead of time if it was out there and I missed it.
I am working on setting up 301 redirects on an apache server to point old pages on a CMS to new pages. During the transition, we need to operate pages on both systems. They have a page that:
if has no GET parameters tacked on the end, should redirect to the new CMS
if it has GET parameters, it should NOT be redirected and should continue working with the old CMS
I have seen many mod_rewrite rules to do the opposite of what I am looking for, but not one that will only activate if there are no GET parameters. I have tried the following, but it did not work:
RewriteCond %{REQUEST_URI} ^/page.php$<br />
RewriteCond %{QUERY_STRING} ^<br />
RewriteRule ^.*$ http://new.domain.com [L,R=301]
If someone goes to old.domain.com/page.php they should be redirected to new.domain.com. If someone goes to old.domain.com/page.php?var=1&var=2, they should remain on this domain and not be redirected.
Thanks, in advance, for any help you could provide,
Dave

Remove links from Google after adding rewrite rules in Apache

I have a website that used to use php "standard" addresses, such as index.php?id=10.
Now I've configured mod_rewrite to have better URLs, therefore i added the rule
RewriteRule ^/([0-9]+)/?$ /index.php?id=$1 [QSA]
Consequently, every time someone surfs to /10 he or she will be redirect to index.php?id=10.
As the redirect is identical to the old address, Google continues to give as results the old php-style URLs, and some results are now duplicated when you perform a search in Google.
I subscribed to Google Webmaster Tools and added a sitemap for the website, leaving only the rewrite-style addresses, but after more than two months the old URLs still appear. Is there a way to remove them from Google?
Thanks a lot!
Redirect incoming old-style URLs to a site returning 404 and google will remove them. Incoming new link's wont be affected as long as you stay with interal rewrites (and exclude the resulkts from being rewritten again, either by having the rule after the others ur uisng the L flag).
This could look like this (untested, but you should at least get the idea):
RewriteRule ^/([0-9]+)/?$ /index.php?id=$1 [QSA,L]
RewriteRule ^/index.php?id=([0-9])$ - [R=404,L]
Sending the permanent redirect status code 301 (plus location header) to redirect to the new URLs should also work, in that case google won't remnove the entries but correct the URL.
RewriteRule ^/([0-9]+)/?$ /index.php?id=$1 [QSA,L]
RewriteRule ^/index.php?id=([0-9])$ /$1 [R=301,L]
You should, however, keep the old style URLs functional anyway, because Cool URIs don't change.

Canonicalizing domain name with mod_rewrite

I had a working .htaccess for my website, but I decided to add domain name canonicalization - e.g. prepend 'www' if it is omitted.
Here's what I'm using now:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteRule ^.*$ http://www.domain.com/$0 [L,R=301]
RewriteRule ^$ http://www.domain.com/blog/ [L,R=301]
// etc
The first condition/rule are the ones I added. The second rule redirects empty filepath to "blog/". Now, the problem is, if I type in a path like "domain.com/page", for some reason it redirects me to the blog. If I change the first rule to http://www.domain.com/test?url=$0, it correctly redirects me to www.domain.com/test?url=page. What am I doing wrong, and is there a way to possibly see some sort of logs/debug output? (I'm using shared hosting so I don't have access to low level logs)
Update: its working fine now; it seems the redirects were cached somewhere, even though clearing all browser data didn't help. Could it be possible that the redirects got cached on proxy level, and how to deal with it, since it makes editing/debugging .htaccess nearly impossible?

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

Domain Redirects SEO relative to the root folder

I got two related problems in the site root.
First, both "domain.com" and "www.domain.com" works the same way. Is that a problem for SEO purposes? Some guy told to use a redirect. Should I do that? What redirect should I use?
Second, my visitors that visit the site over the "/" url get geolocated and redirect to a specfic page, it means, if the visitor access "www.domain.com" from the city a, he gets a redirect to "www.domain.com/a/", if the visitor came from city b, he gets a redirect to "www.domain.com/b/", etc... How should I work over that?
PS: Feel free to rename the question, I dont know how to name it properly.
Thx
First point answer:
Yes, you should redirect it. Otherwise you can run into the duplicated content issue. You can redirect using .htaccess and the code will be this:
RewriteEngine on
RewriteCond %{http_host} ^site\.com [nc]
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,nc,L]
Second point answer:
You can use .htaccess also in this case. But for this question, please bear this discussion in mind (especially my answer).