I am on a wordpress multisite.
I have old urls with the same subfolder name now i have to redirect old url to a new url with new subfolder name.
Example (will be simpler)
Old url 1 to new url 1:
http://site1.fr/fr-chauffage.html to
https://www.site1.fr/installation_chauffage
Old url 2 to new url 2:
http://site2.fr/fr-chauffage.html to https://www.site2.fr/installateur_chauffage_dans_ma_region
So I wrote the following htaccess file
RewriteCond %{HTTP_HOST} ^site1\.fr
RewriteRule ^/?(.*)$ https://www.site1.fr/$1 [R=301,L]
RewriteRule ^fr\.html$ / [R=301,L]
RewriteRule ^fr-chauffage.html$ /installation_chauffage/ [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^site2\.fr
RewriteRule ^/?(.*)$ https://www.site2.fr/$1 [R=301,L]
RewriteRule ^fr\.html$ / [R=301,L]
RewriteRule ^fr-chauffage.html$ /installateur_chauffage_dans_ma_region/ [R=301,NC,L]
The trouble is whatever the website the user is always redirect to site1 + good_subfolder
If I have 3 websites with the same subfolder name the user will be always redirected to site1 + good_subfolder of the new url ...
The trouble is whatever the website the user is always redirect to site1 + good_subfolder
Because the RewriteCond directive only applies to the first RewriteRule that follows. The two subsequent RewriteRule directives are executing unconditionally.
You need to repeat the condition for all rules where it needs to apply.
You're directives are also in the wrong order - you need to have the more specific rules first, otherwise you will get multiple redirects. Currently, you are redirecting everything in your first rule. And then you have a more specific redirect later.
So, are all incoming requests less the www prefix (as suggested in your examples and directives)?
Try the following instead:
# Common rules for both sites
# (Although this will result in 2 redirects if requesting the non-canonical hostname)
RewriteRule ^fr\.html$ / [R=301,L]
# Site 1 rdirects
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.fr [NC]
RewriteRule ^fr-chauffage.html$ https://www.site1.fr/installation_chauffage/ [R=301,NC,L]
# Site 2 rdirects
RewriteCond %{HTTP_HOST} ^(www\.)?site2\.fr [NC]
RewriteRule ^fr-chauffage.html$ https://www.site2.fr/installateur_chauffage_dans_ma_region/ [R=301,NC,L]
# non-www to www redirect - both sites
RewriteCond %{HTTP_HOST} ^(site1\.fr|site2\.fr) [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]
Related
I need to redirect a few (not all) subdomain pages to another address in .htaccess:
How can I redirect the following:
spb.example.com/blog/
ekb.example.com/blog/
spb.example.com/projects/
ekb.example.com/projects/
to
example.com/blog/
example.com/projects/
I've tried:
RewriteCond %{REQUEST_URI} ^spb.example.com/blog/$
RewriteRule ^.*$ https://example.com/blog/? [R=301,L]
But it isn't working.
RewriteCond %{REQUEST_URI} ^spb.mytestsite.com/blog/$
RewriteRule ^.*$ https://mytestsite.com/blog/? [R=301,L]
The REQUEST_URI server variable contains the URL-path only. It does not contain the requested hostname (which is present in the HTTP_HOST server variable).
For example:
# Redirect "spb.example.com/blog/" to "example.com/blog/"
RewriteCond %{HTTP_HOST} ^spb\.example\.com [NC]
RewriteRule ^blog/$ https://example.com/blog/ [R=301,L]
However, you can do all 4 redirects in a single rule by using a more flexible regex and backreferences.
For example:
RewriteCond %{HTTP_HOST} ^(?:spb|ekb)\.example\.com [NC]
RewriteRule ^(blog|projects)/$ https://example.com/$1/ [R=301,L]
The $1 backreference contains either blog or projects from the requested URL-path (captured in the RewriteRule pattern).
NB: Test first with 302 (temporary) redirects to avoid potential caching issues.
UPDATE:
How can I redirect all blog articles from subdomains the same rules For example: from spb.example.com/blog/my-blog-article-1/ ekb.example.com/blog/my-blog-article-1/ to example.com/blog/my-blog-article-1/
You could do it like this by adding a second capturing group to the existing rule above:
RewriteCond %{HTTP_HOST} ^(?:spb|ekb)\.example\.com [NC]
RewriteRule ^(blog|projects)/(.*) https://example.com/$1/$2 [R=301,L]
My website is located in a subfolder (gng2) of my public_html folder. I used the second part (##2) of the below .htaccess file to rewrite the url to add the gng2 subfolder to the URI. It works fine, the app loads when I enter my url to the browser.
Now I added the first part (##1)to redirect any requests where the url contains the subfolder as well. I want www.staging.gonativeguide.com/gng2/en to be redirected to www.staging.gonativeguide.com/en. However this does not work: the first url does not get redirected to the second. Checking the below code on htaccessTester, it says it is correct and it should redirect.
My websited is hosted by a shared hosting service and I think the web server is nginx. Any idea why the redirect does not work?
RewriteEngine On
## 1. redirect request when it contains the gng2 subfolder.
RewriteCond %{HTTP_HOST} ^staging.gonativeguide.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.staging.gonativeguide.com$
RewriteCond %{REQUEST_URI} gng2/
RewriteRule gng2/(.*) www.staging.gonativeguide.com/$1 [R=301,L]
## 2. rewriting url to add the gng2 subfolder containing the app.
RewriteCond %{HTTP_HOST} ^staging.gonativeguide.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.staging.gonativeguide.com$
RewriteCond %{REQUEST_URI} !gng2/
RewriteRule (.*) /gng2/$1 [L]
There are a number of issues with your current attempt. So I sketched an alternatate version which simplifies the rules and enhances robustness:
RewriteEngine On
## 1. redirect request when it contains the gng2 subfolder.
RewriteCond %{HTTP_HOST} ^(www\.)?staging\.gonativeguide\.com$
RewriteRule ^/?gng2/(.*)$ https://www.staging.gonativeguide.com/$1 [R=301]
## 2. redirect request that do not contain the "www" prefix in the host name.
RewriteCond %{HTTP_HOST} ^staging\.gonativeguide\.com$
RewriteRule ^ https://www.staging.gonativeguide.com%{REQUEST_URI} [R=301]
## 3. rewriting url to add the gng2 subfolder containing the app.
RewriteCond %{HTTP_HOST} ^(www\.)?staging\.gonativeguide\.com$
RewriteCond %{REQUEST_URI} !^/gng2/
RewriteRule ^ /gng2%{REQUEST_URI} [END]
You need to make absolutely sure that you are not looking at cached results when testing. So always test using a fresh anonymous browser window and use "deep reloads" (CTRL-F5 typically) instead of just reloading.
I have olddomain.com - and I want to redirect to newdomain.com with htaccess and 301 - thats easy and working very well for me - if I am redirecting whole domain.
But on the new domain I changed few urls (now they are different then on the previous domain) and I want to redirect whole domain and few specific pages to few specific pages and I dont know how to combine this 2 conditions (redirect whole domain and redirect few specific pages).
This is working for me
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ https://newdomain.com/$1 [R=301,L]
and I would like to add to the code some specific redirects like this but I dont know how to combine it together that it will be working:
Redirect 301 /something/ https://newdomain.com/something-changed-new/
Thank you in advance for a help.
Check this rewrites in top of your .htaccess file
RewriteEngine On
RewriteRule ^something\/$ https://newdomain.com/something-changed-new/ [R=301,L]
RewriteRule ^other\/$ https://newdomain.com/something-changed-other/ [R=301,L]
RewriteRule ^old\/$ https://newdomain.com/new/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule (.*) https://newdomain.com/$1 [L]
Here is my Problem
I have 10 URLS on a website A
I want to redirect 2 URLS (out of 10) from Website A to Website B via .htaccess
I want that 4 URLS from the list should never be redirected to anywhere
I want that remaining 4 URLS should be redirected to Website C
And Anyother URL from website A should redirect to Website D
Can any body help ?
Redirect /percussion/21021-crossing-grip-extensions http://myblog.tumblr.com/thisURL
Redirect /percussion/21045-indoor-percussion-circuits http://myblog.tumblr.com/thisURL
Redirect /percussion/21047-26-standard-rudiments-on-social-media http://myblog.tumblr.com/thisURL
RewriteCond %{REQUEST_URI} !^/wind-instruments/38250-hd200-excerpt-video
RewriteCond %{REQUEST_URI} !^/namm-2015-products/49480-gc1ta
RewriteCond %{REQUEST_URI} !^/namm-2015-products/49485-u1ta-2015
RewriteCond %{REQUEST_URI} !^/namm-2015-products/49486-a6r-a-series-2015
RewriteRule ^(.+)$ http://www.codephun.com/$1 [R=301]
You have a huge .htaccess file. I will provide few tips that you can use to fix your problem.
Don't use Redirect directive, just have your rules using RewriteRule itself.
Use THE_REQUEST variable instead of REQUEST_URI as THE_REQUEST variable that represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.
So for the snippet shown above use:
RewriteEngine On
RewriteRule ^percussion/21021-crossing-grip-extensions http://myblog.tumblr.com/thisURL [L,NC,R=302]
RewriteRule ^percussion/21045-indoor-percussion-circuits http://myblog.tumblr.com/thisURL [L,NC,R=302]
RewriteRule ^percussion/21047-26-standard-rudiments-on-social-media http://myblog.tumblr.com/thisURL [L,NC,R=302]
RewriteCond %{THE_REQUEST} !/wind-instruments/38250-hd200-excerpt-video
RewriteCond %{THE_REQUEST} !/namm-2015-products/49480-gc1ta
RewriteCond %{THE_REQUEST} !/namm-2015-products/49485-u1ta-2015
RewriteCond %{THE_REQUEST} !/namm-2015-products/49486-a6r-a-series-2015
RewriteRule ^(.+)$ http://www.codephun.com/$1 [R=301]
# Rest of the Joomla rules come here
I have been trying to redirect old pages for my old site to the corresponding new ones with permanent redirect.
as well as redirecting www.example.com to example.com
I dont seem to able to do both on the same time
at the moment redirects works for correct links from ex www.example.com/correctlink to example.com/correctlink
but only example.com/Info.aspx is redirected to example.com/about-magento-demo-store and NOT www.example.com/Info.aspx
*Update
I want to remove www. AND redirect 40-50 specific adress to new specific adresses. My problem is that the redirect only works if google has saved the old link without Www. IF google has stored a link including www. then my redirect Redirect permanent example.com/tabid/61/CategoryID/13/ProductID/64/Default.aspx /index.php/solpaneler/re does not function –
*Update
my htaccess looks a bit like this
(with a few lines in the end that where present before I started editing)
(i also tried to ad a line Redirect permanent http://www.example.com/Info.aspx http://example.com/about-magento-demo-store but it does not function)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://example.se/$1 [R=301,QSA,L]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%2://%1%{REQUEST_URI} [L,R=301]
Redirect permanent /Info.aspx /about-magento-demo-store
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule .* index.php [L]
Just use rewrite rules for everything. Rewrite rules and Redirect don't always mix well.
RewriteRule ^Info\.aspx$ /about-magento-demo-store [L,R=301]
I've had the same problem a looooooooong time ago. Here's a sample of my rewriterules:
# If domain name doesn't end with .com redirect to .com:
RewriteCond %{HTTP_HOST} (.*)\.(fr|net|org|eu) [NC]
RewriteRule (.*) http://%1.com$1 [R=301,L]
# If domain name without "www", add them:
RewriteCond %{HTTP_HOST} ^mydomainname\.(fr|com|net|org|eu) [NC]
# Ca signifie forcément que c'est sans www => forcer redirection :
RewriteRule (.*) http://www.mydomainname.%1$1 [QSA,R=301,L]
NB: Put this on the top of your rewrite rules, because it should be processed before anything else so that you are sure your domain name always begin with "www"
Note that it redirects mydomainname only if it's "empty" i.e. nothing behind. It won't touch URLs like http://abc.mydomainname.com and it won't touch URLs like http://abc.def.mydomainname.com
Tell me if it works.