.htaccess removing www. AND redirect - .htaccess

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.

Related

.htaccess rewrite to same alias without infinite redirects

I have...
| .htaccess : (v1)
RewriteEngine on
RewriteRule ^in?$ login.php
So, /in --is-really--> /login.php
This much works great. We all can learn how to do this from: .htaccess redirect with alias url
But, I want it to also work in reverse...
If someone should enter /login.php into the address bar, I want it to change to /in.
So also, /login.php --rewrites-to--> /in
From this Answer to a different Question, I want to be ready for anything, using REQUEST_URI. So, my .htaccess file starts with this...
| .htaccess : (v2)
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
# "in" --> login.php
RewriteRule ^in?$ login.php
That also works great.
But now, I want to add this rule (my Question here) for /in <--> /login.php both ways, just how / <--> /index.php already works with .htaccess (v2). So, I adopted the settings and added a second rule...
| .htaccess : (v3) —not working!
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
# "in" --> login.php, and also redirect back to it
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php
RewriteRule (^|/)login\.php(/|$) /%1in [R=302,L]
RewriteRule ^in?$ login.php
...but then /in and /login.php both cause an infinite redirect loop.
What's the right way to do this, still using REQUEST_URI, and still having both rewrite rules (for index.php and for login.php)?
These Questions did not help:
Rewrite rule to hide folder, doesn't work right without trailing slash
This is not about a trailing slash
Allow multiple IPs to access Wordpress Site Admin via .htaccess
This is not about IP-based access
Htaccess URLs redirects are working for http not all https
This is not about https vs http
Rewrite-rules issues : .htaccess
This is not about cleaning up the GET array in the URL
apache htaccess rewrite with alias
This is not about rewriting the host/domain, thereby preserving the path
rewrite htaccess causes infinite loop?
This is not about www subdomain rewrites
.htaccess rewrite page with alias
This is not about rewriting "pretty" URLs nor about how to use slug settings in WordPress
Htaccess alias or rewrite confusion
This is not about simply having multiple rules with the same destination
htaccess rewrite to include #!
I'm not trying to rewrite #!
Reason of redirect loop is a missing RewriteCond %{ENV:REDIRECT_STATUS} ^$ before first redirect rule that removes index.php. Remember that RewriteCond is applicable to immediate next RewriteRule only.
Suggested .htaccess:
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php$ [NC]
RewriteRule ^ /%1 [R=301,L]
# "in" --> login.php, and also redirect back to it
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php$ [NC]
RewriteRule ^ /%1in [R=302,L]
RewriteRule ^in?$ login.php [L,NC]
It won't cause redirect loop because after first rewrite to /login.php, variable REDIRECT_STATUS will become 200 and then the RewriteCond %{ENV:REDIRECT_STATUS} ^$ will stop redirect looping.
Thanks to the help from the user with the correct answer, I found that...
RewriteCond %{ENV:REDIRECT_STATUS} ^$
...doesn't go in .htaccess only once, but every time on the line before...
RewriteCond %{REQUEST_URI} ...

301 redirection whole domain to new one + few individual pages to new urls

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]

Redirect Specific Wildcard Subdomain to a specific url of another domain

I want to redirect specific wildcard subdomain to a specific url of another domain. the both domains are on the same server/public_html, and redirect all the inner pages of the wildcard subdomain to the inner pages of the other domain.
e.g
redirect music.example.com to stackoverflow.com/music/
and
redirect music.example.com/happy-birthday/ to stackoverflow.com/happy-birthday
redirect music.example.com/sing-to-you/ to stackexchange.com/sing-to-you/
I have more than 100k post so i cannot make the redirection of the inner pages one by one using .htaccess
Please help me out,
I tried the below code
RewriteCond %{HTTP_HOST} ^(music.example.com) [NC]
RewriteRule ^()$ https://www.stackoverflow.com/music/ [R=301,L]
but it only redirect the homepage to the specific url which I wanted, but the rest of the urls is the main issue, If I use the normal domain redirection, it works for the rest of the urls, but the function of the code above will stop working
Here is the below code for normal domain redirect redirection
RewriteCond %{HTTP_HOST} ^music\.domain.com\.com [NC]
RewriteRule ^(.*)$ https://wwww.stackoverflow.com/$1 [L,R=301,NC]
I used the below htaccess code and it works perfectly.
RewriteEngine On
# "music.example.com/" to "another.example/music/"
RewriteCond %{HTTP_HOST} ^music\.example\.com [NC]
RewriteRule ^$ https://another.example/music/ [R=302,L]
# "music.example.com/<something>" to "another.example/<something>"
RewriteCond %{HTTP_HOST} ^music\.example\.com [NC]
RewriteRule (.+) https://another.example/$1 [R=302,L]
The RewriteCond (condition) directive checks the requested host. The RewriteRule naturally redirects to the other domain. In the second rule the URL-path from the request is captured in the $1 backreference.

301 redirect to new domain with same url structure

I have a Magento website that is moving to a new domain. Im looking to 301 redirect all pages from the old domain to the new domain keeping same url structure.
I've updated the .htaccess file on my old domain with:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^new-domain.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]
The issue: The above seems to redirect every instance of: old-domain.com/subdir/whatever only just to the main domain: new-domain.com.
I'd be looking to redirect old-domain.com/subdir/whatever to: new-domain.com/subdir/whatever.
Any idea on what could be wrong?
Adjust your rule to use REQUEST_URI variable which is not dependent on the directory of .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://new-domain.com%{REQUEST_URI} [R=301,L,NE]
Better to clear your browser cache before testing this rule.
PS: You need to match hostname condition to old-host not the new-host.
This should redirect and retain the path after the main domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^old-domain\.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]

How to .htaccess redirect domain to subdomain while keeping the main domain intact

I want to redirect all urls under my domain
domain.com/urls, www.domain.com/profile/user1.html etc. to subdomain.domain.com/urls and subdomain.domain.com/profile/user1.html etc.
but I dont want to redirect domain.com and www.domain.com to subdomain.domain.com
Is it possible via htaccess?
Edit: I want to redirect just the internal pages and files only. But leaving the main domain.com intact.
More Examples
domain.com/page1.html to subdomain.domain.com/page1.html
www.domain.com/members/admin.html to subdomain.domain.com/members/admin.html
www.domain.com to www.domain.com (no redirection here)
Try:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/.+
RewriteCond %{REQUEST_URI} !^/index\.(php|htm)
RewriteCond %{HTTP_HOST} !^subdomain.domain.com$ [NC]
RewriteRule ^ http://subdomain.domain.com%{REQUEST_URI} [L,R]
If you want to permanently redirect, change the square brackets to: [L,R=301].
You can first handle the main page by not rewriting and redirect everything else with a second rule
RewriteRule ^$ - [L]
RewriteCond %{HTTP_HOST} !^subdomain.domain.com$
RewriteRule ^.+$ http://subdomain.domain.com/$0 [R,L]
When everything works as you expect, you can change R to R=301.

Resources