These are my current directives for redirecting subdomain HTTP requests to a domain with a path.
RewriteCond %{HTTP_HOST} ^blog.website.com
RewriteRule ^(.*)$ http://website.com/blog [L]
However, this site is a new version of a website for which social media links have been posted. These links have image URLs which are now broken since the URL now points to a different server. The images have since been migrated and the path to said images has been preserved on the new server. In light of this, I need rewrite my directives to account for these legacy links.
I need to write conditions that rewrite the URL only when the URL no path or when the first parameter of the URL is not /wp-content/.
I have to make conditions and rules that differentiate between these 3 URL patterns:
http://blog.website.com
-> http://website.com/blog
http://blog.website.com/wp-content/year/month/image.jpg
-> http://website.com/wp-content/year/month/image.jpg
http://blog.website.com/entry-url-title
-> http://website.com/blog/entry-url-title
This is ultimately how I was able to do it, with a little help from my friends, of course.
RewriteCond %{HTTP_HOST} blog.website.com
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^(.*) http://website.com/blog [R=301,L]
RewriteCond %{HTTP_HOST} ^blog.website.com
RewriteCond %{REQUEST_URI} !wp-content
RewriteRule ^(.*) http://website.com/blog/%{REQUEST_URI} [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^blog.website.com
RewriteCond %{REQUEST_URI} wp-content
RewriteRule ^(.*) http://website.com/%{REQUEST_URI} [R=301,NC]
Related
I am trying to redirect all URLs on my site (let's call it www.site1.com) except one in my .htaccess file. I currently have the following in my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/my/page$ [NC]
RewriteRule (.*) https://www.site2.com [R=302,L]
With the above, all requests to www.site1.com are redirected to www.site2.com, including the one that I do not want to redirect.
After some experimentation, I have found that the following works to redirect only a specific page:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/my/page$ [NC]
RewriteRule (.*) https://www.site2.com [R=302,L]
I'm not sure why the ! operator isn't working as I expect. Perhaps there is an error in my regex?
Some additional bit of information. This is a Drupal site running on a dedicated cPanel host. But I have been sure to put this new redirect rewrite rule before all the other Drupal-specific rewrite rules.
Any help would be greatly appreciated.
RewriteCond %{REQUEST_URI} !/(index.php) [NC]
RewriteRule ^(.*)$ index.php [NC,QSA,L,END]
You need to make sure you exclude the page you redirect to from you list of possible url that get redirected(to avoid the loop) and then you can add more before the RewriteRule.
RewriteCond %{REQUEST_URI} !/(index.php) [NC]
RewriteCond %{REQUEST_URI} !/(index_new.php) [NC]
RewriteRule ^(.*)$ index.php [NC,QSA,L,END]
I all, I've been searching a lot but I don't find an answer to my problem.
I have already made a .htaccess file that redirects all the different pages and files on my old domain (www.olddomain.com) to my new domain (www.newdomain/blog.com). I'm moving a blog to an e-commerce, that's why 301 to www.newdomain/blog.com.
However, I need to use the Change of Address Tool (Google Webmaster Tools) but I can't because www.olddomain.com must redirect to www.newdomain.com and now it is redirecting to www.newdomain/blog.com and Google don't let me change the address.
My .htaccess is working fine except for that, here it is
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule (.*)$ http:/www.newdomain.com/blog/$1 [R=301,L]
BUT, I have not found any way to make an exception to redirect just www.olddomain.com must ---> www.newdomain.com
I have tried to make that exception in the following way:
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^/$ http://www.newdomain.com/ [R=301,L]
RewriteRule (.*)$ http://www.newdomain.com/blog/$1 [R=301,L]
But nothing has worked...
Please, can you help me?
Thanks in advance
Try :
RewriteEngine On
#redirect http://oldsite.com/ to http://newsite.com
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^$ http:/www.newdomain.com/$1 [R=301,L]
#redirect http://oldsite.com/pages to http://newsite.com/blog/pages
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^(.+)$ http:/www.newdomain.com/blog/$1 [R=301,L]
Note the ^$ in pattern it matches / ie : http://example.com/ or the directory the htaccess file is in.
Clear your browser cache before testing this.
I'm trying to redirect all urls from one domain to another but one (kind of). This is the htaccess I have to redirect all keeping the same url except of the domain (for example domain.com/something goes to domain2.com/something).
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^(.*)$ "http://domain2.com/$1" [R=301,L]
What I want to know is how to redirect all except if the url is domain.com/validation/*
/validation/ is not a subfolder and it has to be the next part of the url after the domain (domain.com/something/validation can redirect, and domain.com/validation/something can't).
I tried a lot of options but none worked :(
I hope is enough information.
You can exclude it in RewriteRule itself:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule !^validation http://domain2.com%{REQUEST_URI} [NE,NC,R=301,L]
I have the following setup:
-> domain1.com is my main domain and also document root
-> domain2.com is another domain which is registered as an alias domain (means it does exactly the same as domain1.com), I have multiple of those domains.
when I now want to redirect that domain to something I usually use this to redirect the domain to something else (like an external website):
RewriteCond %{HTTP_HOST} ^(www\.)?domain2.com$ [NC]
RewriteRule ^(.*) http://somewiredisp.com/user/bla/$1 [L,R]
this is usually used if I register a domain for someone who needs a nice url for their forums or whatever. Works like a charm - the only "problem" would be that the address bar in the browser changes, but I've read that there would be no way to do that with external URLs, and since no one ever complained about it I am fine with it.
However, now I would like to link some url to a subdirectory WITHOUT the url changing in the address bar.
domain2.com -> domain1.com/subdir (or domain2.com/subdir - that doesn't matter, since it's an alias domain).
my current approach would be
RewriteCond %{HTTP_HOST} ^(www\.)?domain2.com$ [NC]
RewriteRule ^(.*) http://domain1.com/subdir/ [P]
which doesn't work (Error 404) - if I call domain1.com/subdir/ directly if works obviously.
I also tried several variations of
RewriteRule ^(.*) http://domain2.com/subdir/ [P]
RewriteRule ^(.*) /subdir/ [P]
RewriteRule ^(.*) http://domain1.com/subdir/index.html [P]
all with the same result.
maybe it's just a simple mistake but I am currently clueless :/
You can use this rule:
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule !^subdir/ /subdir%{REQUEST_URI} [L,NC]
This will prefix every request for domain2.com with /subdir if it is not already there.
Below code works perfectly for me.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/folder_name/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder_name/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ folder_name/index.html [L]
Show subdirectory pages without changing url in root domain
Change according to your need subfolder_name in code
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteRule ^(.*)$ /subfolder_name/$1
</IfModule>
We have a multi-subdomain site that generates dynamic cotent depending on the subdomain text. However it does not work if www is appended to the subdomain. As some users are used to add www in front of every URL, we would like to fix it with a URL rewrite.
EDIT
I have got this far:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.subdominio\.dev [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{http_host} ^www\.([^\.]+)\.subdominio\.dev [NC]
RewriteRule ^(.*) http://%1.subdominio.dev$1 [R=301,QSA,NC]
Surprisingly. It works well in one of my test subdomains, but not in the other:
www.otro-mas.subdominio.dev gets redirected to otro-mas.subdomino.dev (with and without an URI like /index.html). Just as expected.
However www.ono.subdominio.dev is going into an infinite redirect. Like this:
http://www.ono.subdominio.dev/ono.subdominio.dev//ono.subdominio.dev//ono...
Why is it not rewriting the host?
Try this:
RewriteCond %{HTTP_HOST} ^www\.([a-zA-Z-_]+)\.domain\.com [NC]
RewriteRule ^(.*)$ http://%1\.domain\.com/$1 [R=301,NC,QSA,L]
You probably forgot the QSA and the L directives (search on the Apache mod_rewrite documentation for the explanation).
It does work.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.subdominio\.dev [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{http_host} ^www\.([^\.]+)\.subdominio\.dev [NC]
RewriteRule ^(.*) http://%1.subdominio.dev$1 [R=301,QSA,NC]
I had some problem with the caches in my browser.