How to fix 301 redirect that is not working - apache

I'm trying to create a redirect from https://www.compareking.no/penger/kredittkort to https://www.compareking.no/forbrukslaan but nothing is happening when I modify the htacess file.
This is the rewrite rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.compareking.no/penger/kredittkort$ [NC]
RewriteRule ^(.*)$ http:// www.compareking.no/forbrukslaan/$1 [L,R=301]
Thoughts?

HTTP_HOST only matches the HOST/domain part of the URL, that specified in RewriteCond and the URI prefix in RewriteRule would get you on the right track, as shown below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.compareking\.no$ [NC]
RewriteRule ^penger/kredittkort(.*)$ http://www.compareking.no/forbrukslaan$1 [L,R=301]

Related

Using .htaccess to redirect example.(us|es) to sub domain (us|es).example.com

I recently purchased a new domain for my site and I want to redirect anyone who visits using an old domain to the new one.
For instance, if they went to either of these:
http://www.example.com -- NOT REDIRECT
http://example.com -- NOT REDIRECT
http://www.example.us --> http://us.example.com -- REDIRECT
http://www.example.es --> http://es.example.com -- REDIRECT
http://example.us --> http://us.example.com -- REDIRECT
http://example.es --> http://es.example.com -- REDIRECT
How would you do this simple .us|.es -> sub domain redirect with a .htaccess file? Any rule should apply to all URLs .
Thanks in advance.
Try this:
RewriteCond %{HTTP_HOST} example.us$
RewriteRule (.*) http://us.example.com [L,R=301]
RewriteCond %{HTTP_HOST} example.es$
RewriteRule (.*) http://es.example.com [L,R=301]
I found this solution
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ example\.us$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.us$
RewriteRule ^/?$ "http\:\/\/us\.example\.com" [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.es$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.es$
RewriteRule ^/?$ "http\:\/\/es\.epys\.com" [R=301,L]
RewriteCond %{HTTP_HOST} example.(us|es)$
RewriteRule (.*) http://%1.example.com [L,R=301]
Just like $1 refers to pairs of brackets in the RewriteRule line, the %1 refers to those in the RewriteCond

htaccess Redirect a Specific Directory URLs to another different URL?

With .htaccess how to redirect from:
http://apple.example.com/old_folder
http://apple.example.com/old_folder/
http://apple.example.com/old_folder/bla/bla/bla?a=b&etc
whatever depths under apple.example.com/old_folder --> to:
http://banana.example.com/new_folder/
.. only?
I used following code, but not working:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^apple.example.com/old_folder$
RewriteRule ^(.*)$ http://banana.example.com/new_folder/ [R,L]
You cannot match URI in RewriteCond %{HTTP_HOST} condition.
This rule should work from root .htaccess of apple.example.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^apple\.example\.com$ [NC]
RewriteRule ^old_folder(/|$) http://banana.example.com/new_folder/? [R,L]
EDIT: OR else use this rule in /old_folder/.htaccess:
RewriteEngine on
RewriteRule ^ http://banana.example.com/new_folder/? [R,L]

Apache redirect subdomain to folder, keep parameters

I have this code in .htaccess :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$
# If empty subdomain, replace with "www"
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*) http://www.example.com/$1 [QSA,L,R=301]
# If subdomain isn't empty and not "www", redirect to "folder"
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301]
#PAGES REDIRECTION
RewriteRule ^(.*)/register/ /index.php?sub=$1&page=register
RewriteRule ^(.*)/register /index.php?sub=$1&page=register
RewriteRule ^(.*)/lostpass/ /index.php?sub=$1&page=lostpass
RewriteRule ^(.*)/lostpass /index.php?sub=$1&page=lostpass
...
(a rule for wildcard subdmains is already in place and working)
If I browse to http://test.example.com it redirects correctly to http://www.example.com/test but when I try to browse to http://test.example.com/register, it actually redirect to http://www.example.com/test/index.php?sub=http://www.example.com/test&page=register which should redirect to http://www.example.com/test/register
What am I doing wrong here? Thanks in advance!
Try adding the L flag to your second redirect rule, similar to how have it in the first.
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301,L]
It looks like your rewritten URI is passing through to the next rule.
Also, I don't think your first two RewriteCond are in the correct spot.

.htaccess rewrite subdomain to directory

Is it possible to use .htaccess to rewrite a sub domain to a directory?
Example:
http://sub.domain.example/
shows the content of
http://domain.example/subdomains/sub/
Try putting this in your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.example
RewriteRule ^(.*)$ /subdomains/sub/$1 [L,NC,QSA]
For a more general rule (that works with any subdomain, not just sub) replace the last two lines with this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.example
RewriteRule ^(.*)$ subdomains/%1/$1 [L,NC,QSA]
I'm not a mod_rewrite expert and often struggle with it, but I have done this on one of my sites. It might need other flags, etc., depending on your circumstances. I'm using this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdomains/subdomain
RewriteRule ^(.*)$ /subdomains/subdomain/$1 [L]
Any other rewrite rules for the rest of the site must go afterwards to prevent them from interfering with your subdomain rewrites.
You can use the following rule in .htaccess to rewrite a subdomain to a subfolder:
RewriteEngine On
# If the host is "sub.domain.example"
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
# Then rewrite any request to /folder
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
Line-by-line explanation:
RewriteEngine on
The line above tells the server to turn on the engine for rewriting URLs.
RewriteCond %{HTTP_HOST} ^sub.domain.example$ [NC]
This line is a condition for the RewriteRule where we match against the HTTP host using a regex pattern. The condition says that if the host is sub.domain.example then execute the rule.
RewriteRule ^((?!folder).*)$ /folder/$1 [NC,L]
The rule matches http://sub.domain.example/foo and internally redirects it to http://sub.domain.example/folder/foo.
Replace sub.domain.example with your subdomain and folder with name of the folder you want to point your subdomain to.
I had the same problem, and found a detailed explanation in http://www.webmasterworld.com/apache/3163397.htm
My solution (the subdomains contents should be in a folder called sd_subdomain:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} subdomain\.domain\.example
RewriteCond $1 !^sd_
RewriteRule (.*) /sd_subdomain/$1 [L]
This redirects to the same folder to a subdomain:
.httaccess
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.example$ [NC]
RewriteRule ^(.*)$ http://domain\.example/subdomains/%1
Try to putting this .htaccess file in the subdomain folder:
RewriteEngine On
RewriteRule ^(.*)?$ ./subdomains/sub/$1
It redirects to http://example.org/subdomains/sub/, when you only want it to show http://sub.example.org/.
Redirect subdomain directory:
RewriteCond %{HTTP_HOST} ^([^.]+)\.(archive\.example\.com)$ [NC]
RewriteRule ^ http://%2/%1%{REQUEST_URI} [L,R=301]
For any sub domain request, use this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.band\.s\.example
RewriteCond %{HTTP_HOST} ^(.*)\.band\.s\.example
RewriteCond %{REQUEST_URI} !^/([a-zA-Z0-9-z\-]+)
RewriteRule ^(.*)$ /%1/$1 [L]
Just make some folder same as sub domain name you need.
Folder must be exist like this: domain.example/sub for sub.domain.example.
Edit file .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Rewrite URL using Apache for redirecting root domain

I have this scenario where I would like to redirect my domains using the following scenario. Can you please advice if this can be achieved using RewriteRule in Apache?
I would like to redirect any calls made using http://www.domainname.com/url to redirect to http://domainname.com/url.
I was able to achieve the above using the following rewrite rule
# BEGIN WithoutWWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]
# END WithoutWWW
If anyone tries to visit just http://www.domainname.com or http://domainname.com, I would like them to redirect to http://dname.com
How can I achieve this rule using RewriteRule in Apache?
I'm also using PHP, so a solution using PHP would be valid too.
Here is your combined .htaccess with your existing and new code:
RewriteEngine on
# redirect domainname.com/ or www.domainname.com/ to dname.com/
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.com$ [NC]
RewriteRule ^$ http://dname.com [R=301,L]
# append www to domainname.com
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
RewriteRule ^ http://domainname.com%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]
That example looks like mod_rewrite.
With mod_rewrite you could do:
# BEGIN WithoutWWW
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.com$ [NC]
RewriteRule ^(.*)$ http://dname.com/$1 [R=301,L]
# END WithoutWWW
You may want to look at http://www.workingwith.me.uk/articles/scripting/mod_rewrite
Based on your comment you want to redirect the root differently so we could do:
RewriteEngine on
#www.domainname.com & domainname.com -> dname.com
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.com$ [NC]
RewriteRule ^$ http://dname.com/ [R=301,L]
#www.domainname.com/[url] -> domainname.com/[url]
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$ [NC]
RewriteRule ^(.*)$ http://domainname.com/$1 [R=301,L]