Strange behavior of htaccess redirect - apache

My goal is to redirect an entire domain to another. Every URL of the old domain shall redirect to the root URL of the new domain.
To achieve this I'am doing:
redirect 301 / http://www.google.de/
Problem is that when I test it on http://localhost/randomPath then it redirects to http://www.google.de/randomPath, but not to the root URL of google.de.
Does anyone know how to solve this?

Use mod_rewrite for finer control on rules like matching Host name etc.:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ http://www.google.de/? [L,R]
? in the end of target URI will strip off any existing query string.

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.

htaccess - Redirecting all traffic to another domain except existing 301's

My htaccess file is filled with 301 redirects like such:
Redirect 301 /old-page.html https://www.example.com/new-page
There are about 100 of these redirects. What I would like to do is redirect all traffic going to the old site to go to the new site excluding the existing 301's
So if someone goes to old-site.com/old-page.html it will take them to new-site.com/new-page and if someone goes to old-site.com/random-page.html it will take them to new-site.com - just the home page.
Is it possible to do this using mod_rewrite and mod_alias without rewriting the current 301's?
You can keep all your 301 rules. Just insert this generic 301 rule below your existing rule:
# all existing 301 rules go here
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?old-site\.com$ [NC]
RewriteRule ^ http://new-site.com/? [L,R=301]
You need to use a RewriteCond in front of all your rules like this:
RewriteCond %{HTTP_HOST} ^(?:www\.)domain\.com$ [NC]
If you want all the following rules to be processed as well DO NOT use the L (last) flag in the RewriteCond statement.
Source: Redirect all urls exactly, just change domain name

How to get domain name from URL and redirect to respective folder using rewrite rule

In my application when the url hit like
http://monohar.opens.com
it should redirect to
http://xx.xxx.xx/folder/manohar
Using htaccess rewrite rule , here domain 'monohar' will be dynamic domain name , could be changed to any thing dynamically but url always ends with 'opens.co'
Please suggest me how to achieve this
You can use this rule in your site root .htaccess or Apache config:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.opens\.com$ [NC]
RewriteRule ^ http://xx.xx.xx/folder/%1%{REQUEST_URI} [L,NE,R=301]
%1 is back-reference of substring before first DOT in HTTP_HOST variable.

301 Redirect not working on particular URL

I have a redirect rule set up as below
Redirect /Products.aspx?Category_ID=15 https://www.trainerbubble.com/free-training-resources/
However, when going to the address it attaches itself to an existing page /training-products and results in this
https://www.trainerbubble.com/training-products/?Category_ID=15
How can I force the original redirect and make it stop thinking its part of the /training-products page?
You cannot match query string in Redirect directive. Use mod_rewrite rules instead:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^Category_ID=15$ [NC]
RewriteRule ^Products\.aspx$ https://www.trainerbubble.com/free-training-resources/? [L,NC,R=301]
? at the end of target URL is to strip off previous query string.

htaccess redirect without the first folder

So we have a system in place for dynamic content for clients.
Essentially they have their own domain. But we also store the content on our domain for deployment.
An example:
Our domain: http://domain_one.com/client_domain/home.php
Their domain: http://client_domain.com/home.php
We need to redirect to the client domain which we can place into the htaccess with php.
What we want to do is also redirect the query string. When we add the query string, it redirects the client_domain/home.php to the client_domain.com
Our rewrite url as follows:
RewriteRule !^(template_files)($!|/) http://".$domain."/ [R=301,L]
This file gets created dynamically via php for those asking about the ".$domain." bit.
You can use this rule:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/(template_files|web_images)/ [NC]
RewriteRule ^[^/]+/(.*)$ http://domain.com/$1 [R=301,NC,L]