Apache 301 redirect only for domains, not pages - apache

I have a 301 redirect to set up for my domain (http://old-site.com and http://www.old-site.com).
I would like to redirect only if nothing appears after the domain in URL.
http://old-site.com redirect to http://www.new-site.com
http://www.old-site.com redirect to http://www.new-site.com
http://old-site.com/article/article-1.php DO NOT redirect
I have tried this but it always redirect, even if I directly call an article:
RewriteCond %{HTTP_HOST} ^(www\.)?old-site\.com
RewriteRule ^(.*)$ http://www.new-site.com/ [R=301]
It should be easy to set up, but I am a real newbe in this.
Thanks for your help.

Of course it redirects everything, because you matched “everything” with (.*).
Try matching for “nothing” instead, with just ^$.

Related

Redirecting some sub pages to another format page using htaccess

I would like to add 301 redirects to specific type subpage on my site which is hosted on LAMP server.
for example,
example.com/witcher-3-100309/player-count - example.com/witcher-3-100309?tab=player-count
example.com/dota-2-100209/player-count - example.com/dota-2-100209?tab=player-count
example.com/pubg-300100/player-count - example.com/pubg-300100?tab=player-count
Is there any way in htaccess to write a general rule for all these type URLs to redirect correctly instead of individual 301 redirect codes in htaccess.
Thanks in advance.
The following should do it
RewriteEngine on
RewriteCond %{THE_REQUEST} /.+\?=([^\s]+) [NC]
RewriteRule ^.+$ %{REQUEST_URI}?tab=%1 [NE,L,R]
This will redirect /foobar/?q=value to /foobar/?tab=value .
When you sure the rule is working ok you can change the R (Temp Redirect flag) to R=301 to make the redirection permanent.

Error when redirecting a domain

The thing is I have one domain in different languages, and I´m not able to do some redirects like this:
I have Spanish http://www.domain.es/inicio
And English http://www.domain.en/inicio
Now, I need to move my page http://www.domain.en/inicio to http://www.domain.en/home
Both domains are using the same .htaccess and I´m not able to do a simple:
Redirect 301 /inicio http://www.domain.en/home
Cause that will redirect the spanish /inicio to /home too.
Also tested
RewriteCond %{HTTP_HOST} ^www.domain.en/inicio/$ [NC]
RewriteRule ^(.*)$ http://www.domain.en/home/ [R=301,L]
But thats also failing.
Did you try something like this?
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /inicio/
RewriteRule ^inicio/(.*) /home/$1 [L,R=301]
You should redirect the content of the folder /inicio to folder /home if I got it right. There is no need to redirect everything since the domain is the same
The request should be GET since the users are accessing your website pages

htaccess subpage on subdomain to subpage on other subdomain

I have trouble finding out the right approach to make a 301 redirect from one subdomains page to another subdomains page.
For instance:
subpage1.mysite.com/contact
needs to be redirected to
subpage2.mysite.com/kontakt
(i have a few hundred of these)
I have a lot of 301 redirects in my .htaccess so far, but they only redirect from the maindomain mysite.com/page.
I cant make a 301 like this, right?
It does not work..
301
Redirect 301 http://subpage1.mysite.com/contact http://subpage2.mysite.com/kontakt
I only can access the primary htaccess file on the main domain (mysite.com)
Read some about RewriteCond but it seems they take all of my subpages and redirect them to the other subdomain. That is not exactly suitable for me, as the url's have changed names and are not identical on the subdomains.
I've figured it out by using this.
Taking the subdomain as RewriteCond
#Consultancy redirects
RewriteCond %{HTTP_HOST} ^consultancy.madebydelta.com [nc]
RewriteRule ^about/$ http://testlab.madebydelta.com/om-os/ [R=301,NC,L]
RewriteRule ^industries/$ http://testlab.madebydelta.com/cases/ [R=301,NC,L]

Basic URL rewrite with mod_rewrite

I would like to rewrite the URL of one of my pages from
http://www.mydomain.com/some/application/page.html
to
http://www.mydomain.com/apply
I believe this code will work. But in 301 redirects, you often see [R=301,L] or some version of that appended to the end of the rewrite rule - is the code below the best way to perform the redirection and will Google understand it?
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /some/application/page.html?=$1 [L]
I think you want to do this:
The URL that your users see (even google):
http://www.mydomain.com/apply
to
http://www.mydomain.com/some/application/page.html
the internal(actual) URL.
then I would suggest you to go this:
RewriteEngine On
#condition to redirect
RewriteCond %{REQUEST_URI} ^/?apply/?$
RewriteRule ^/?apply/?$ /some/application/page.html [L]
The flag [L] ( Flag L docs ) signifies RewriteEngine to stop rewriting any rules further. This will not perform a permanent redirection.
You don't need a [R=301] flag here. 301 is for permanent redirection. Use the short URL everywhere.
To my knowledge, you will need [R=301,L]to do a redirect properly. Apache2 by default will use a 302 redirect so if it is a permanent redirect you should force R=301, as you noted. The documentation for RewriteRule is unclear if [L] alone will always perform a 301 redirect. Be safe, tell apache exactly what to do :-).

htaccess URL Rewrite not working after 301 redirect

I have the following url rewrite:
RewriteRule ^info/([^/\.]+)/?$ info.php?page=info&subpage=$1 [L]
Originally the parameters were the page id's i.e. 0-10.
I have now changed this so the URLs have more meaningful slug names to reflect the content.
I have now set up the 301 redirects, for example:
Redirect 301 /info/0 http://www.example.com/info/intro
But the problem is, the redirect doesn't go to the url rewrite (http://www.example.com/info/intro). Instead it shows the full url (http://www.example.com/info.php?page=info&subpage=0)
How can it get it to keep the rewrite?
Many thanks
As posted by LazyOne, the solution was to use RewriteRule:
RewriteRule ^info/0$ http://www.example.com/info/intro [R=301,L]