Htaccess www redirect - status codes - apache

i am using this rewrite rule in my htaccess:
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
basically it redirects the user to www if he type the adress without the www. (sub-domins excluded).
Is there any other way to do it without htaccess file?
My Problem:
If i navigate the Browser to my Page The first HTTP-Satus code i get is the (www) redirect 301 and then the 200 (for the www.domain.com).
If i try this with, for example. ebay.de the first status code i get is "Pending" and then i get redirected to www.ebay.de with 200.
How they do the www redirect without a 301 htaccess?

Related

.htaccess - redirect, if a specific url match

I need to redirect users to a new domain, but I need that in case a user visits a particular URL, then he/she needs to be redirected to another URL.
Let me clarify...
If the user visits http://oldexample.com/postvendita/ I need to redirect them to http://newexample.com/assistenza
Otherwise, for every other URL http://oldexample.com/* I need to redirect them to http://newexample.com/new-page
Here is my attempt:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)postvendita(.*)$ http://www.newexample.com/assistenza [L,R=301]
RewriteCond %{HTTP_HOST} !^oldexample\.com$ [NC]
RewriteRule ^(.*)$ http://www.newexample.com/new-page [R=301,L]
Now if I visit any of the old pages, I will be redirected to http://www.newexample.com/new-page, so the first rule doesn't work, how should I change it?
To handle this via .htaccess you'll want to match the first one and use a catch-all to redirect everything else:
RewriteEngine On
RewriteRule ^postvendita/?$ http://www.newexample.com/assistenza [R=301,L,NE]
RewriteRule .* http://www.newexample.com/new-page [R=301,L,NE]
A 301 redirect is a permanent redirect which passes between 90-99% of
link juice (ranking power) to the redirected page. 301 refers to the
HTTP status code for this type of redirect. In most instances, the 301
redirect is the best method for implementing redirects on a website.
More About Redirects
Alternatively, if you're not comfortable writing RewriteRules, you can use the following lines:
Redirect 301 /postvendita/ http://www.newexample.com/assistenza
Redirect 301 /postvendita http://www.newexample.com/assistenza
Redirect 301 / http://www.newexample.com/new-page
Or with RedirectMatch:
RedirectMatch 301 /postvendita/? http://www.newexample.com/assistenza
RedirectMatch 301 .* http://www.newexample.com/new-page
Common .htaccess Redirects

htaccess in subdirectory redirects to main domain

I have the following rules for url management which redirect non-www traffic to www and then rewrite some urls. It seems to work fine except when i go to "http://example.com/prosite/" (without www, the first redirection case - "^$"). I would expect this to bring the home page (meaning it should redirect to http://www.example.com/prosite/index.php?page=home) but instead it redirects me to "http://www.example.com". Thanks for your help.
RewriteEngine On
RewriteBase "/prosite/"
# redirect non-www addresses to www
# redirect condition: hostname does NOT contain 2 or more dots
RewriteCond "%{HTTP_HOST}" "!\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])\."
RewriteRule "^(.*)$" "http://www.%{HTTP_HOST}/prosite/$1" [R=301,L]
# redirect empty path to home page
RewriteRule "^$" "index.php?page=home" [NC,L]
# redirect page path to corresponding page
RewriteRule "^(home)/?$" "index.php?page=$1" [NC,L]
# redirect all other paths to index.php (without a "page" argument this will show the default page: 404 not found)
RewriteRule "!^index\.php$" "index.php" [NC,L]
Apparently it was a browser cache problem. I emptied the cache and it worked.

htaccess redirect on a domain that already redirects

We have 3 domains in play here. Domain1.com is the main site, domain2.com is the smaller second site, and domain3.com is a vanity URL that redirects from domain1 to a Spanish version of domain2.
What we need to have happen is for domain3.com/subpage to redirect to domain2.com/subpage, however the original redirect of the vanity URL is overriding any attempts we make.
Is this kind of redirect possible?
Here is the original redirect in domain1.com's htaccess file:
RewriteCond %{HTTP:Host} ^www\.domain3\.com$
RewriteRule (.*) http\://www\.domain2\.com/spanish-version [I,R]
These are the attempts at making the needed redirect happen (also in domain1.com's htaccess file):
RewriteCond %{HTTP:Host} ^www\.domain3\.com/subpage$
RewriteRule (.*) http\://www\.domain2\.com/spanish-version/subpage [I,R]
RewriteRule ^domain3\.com/subpage?$ /spanish-version/subpage [NC,R=302,U]
Rules for your domain3 should look like this :
RewriteCond %{HTTP:HOST} ^(www\.)?domain3\.com$ [NC]
RewriteRule ^/?(subpage_name)/?$ http://www.domain2.com/$1 [NC,R,L]
This will redirect
www.domain3.com/subpage
to
www.domain2.com/subpage
(www.)? in HTTP:HOST variable is to make the "www" part optional in url, so in this case
urls with or without "www" would also work.

.htaccess - Redirect subdomain to main domain (only if NOT xyz.php)

Hi I would like to redirect the following sobdomain:
subdomain.domain.com to domain.com
ONLY if the address/script is NOT subdomain.domain.com/xyz.php
some examples:
subdomain.domain.com/index.php -> 301 redirect to domain.com/index.php
subdomain.domain.com/123.php -> 301 redirect to domain.com/123.php
subdomain.domain.com/xyz.php (no redirect)
I tried to redirect with the following code but I'm struggling with the condition. The redirect itself works... however for all pages:
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.co.uk$ [NC]
RewriteRule !^subdomain.domain.co.uk/xyz.php$ domain.co.uk%{REQUEST_URI} [NE,R=301,L]
Finally I found a solution which works. if someone struggles with the same issue you can try this .htaccess code:
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com$ [NC]
RewriteRule !^xyz\.php($|/) http://www.domain.com%{REQUEST_URI} [NE,R=301,L]
The first line addresses the subdomain (case insensitive)
The second line redirects all sites Except the specified xyz.php to the main domain (301)

Htaccess 301 only part of the redirect works

Hi Im moving a site from one domain to another, and I have created the following .htaccess file, but its not working.
*#Options +FollowSymlinks
RewriteEngine On
redirect 301 http://www.el-netshop.dk/pi/Dækkape_UG150_12_lysegrå_5302_.aspx http://www.el-netsalg.dk/pi/Dækkape_UG150_12_lysegrå_5271_.aspx
RewriteCond %{HTTP_HOST} ^el-netshop.dk$ [OR]
RewriteCond %{HTTP_HOST} ^www.el-netshop.dk$
RewriteRule (.)$ http://www.el-netsalg.dk/$1 [R=301,L]
I would like it to work like this.
Have a list of urls where the url is diffent, with more then just the domain. Ex. in the above the from link contains 5302 but to link is 5271.
Then with the rest, I want it to make a normal redirect.
The above code just do (.*)$ http://www.el-netsalg.dk/$1 and ignores the special cases.
What am I doing wrong?
According to the apache docu the syntax is as folows:
Redirect 301 /service http://foo2.bar.com/service
So try:
Redirect 301 /pi/Dækkape_UG150_12_lysegrå_5302_.aspx http://www.el-netsalg.dk/pi/Dækkape_UG150_12_lysegrå_5271_.aspx
without the "http://www.el-netshop.dk" for the old-path paramater.