mod rewrite to change the domain - .htaccess

I am trying to change the domain of my urls, Parent URL is something like
http://abc.def.com/123123132/abcvalue
I have a rule like
RewriteCond %{HTTP_HOST} ^abc\.def\.com$
RewriteRule ^(.*)$ https://efg.com/dd/as/$1 [L]
this is changing the domain but it is putting the old domain in new url too. Something like
https://efg.com/dd/as/http://abc.def.com/123123132/abcvalue
Need help on this. Thanks
Edit:- Complete htaccess is
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
RewriteEngine On
#### PrintNinja unique redirects
RewriteCond %{HTTP_HOST} ^printninja.com$ [NC]
RewriteRule ^learn/(.*) /printing-resource-center/ [NC,L,R=301]
RewriteRule ^digital-downloads/(.*) /printing-resource-center/ [NC,L,R=301]
RewriteRule ^resources/(.*) /printing-resource-center/ [NC,L,R=301]
RewriteRule ^pricing\/(.*)$ https://printninja.com/calculator/$1 [L]
#### Begin Uploader rewrites
# printingupload.com/files-submited/* --redirect to -> https://printninja.com/calculator/files-submited/*
RewriteCond %{HTTP_HOST} ^(www\.)?printingupload\.com$
RewriteRule ^printingupload\.com/files-submited\/(.*)$ https://printninja.com/calculator/files-submited/$1 [L]
# printingupload.com/invalid-order/* --redirect to -> https://printninja.com/calculator/invalid-order/*
RewriteCond %{HTTP_HOST} ^(www\.)?printingupload\.com$
RewriteRule ^printingupload\.com/invalid-order\/(.*)$ https://printninja.com/calculator/invalid-order/$1 [L]
# printingupload.com/* --redirect to -> https://printninja.com/calculator/products/upload_proofing/* (this rule must appear after the other printingupload.com/* rules)
RewriteCond %{HTTP_HOST} ^(www\.)?printingupload\.com$
RewriteRule ^printingupload\.com\/(.*)$ https://printninja.com/calculator/products/upload_proofing/$1 [L]
# submit.printingupload.com/* --redirect to -> https://printninja.com/calculator/submit_proofing/*
RewriteCond %{HTTP_HOST} ^submit\.printingupload\.com$
RewriteRule ^submit\.printingupload\.com\/(.*)$ http://printninja.com/calculator/submit_proofing/$1 [L]
# proofs.printingupload.com/approved/* --redirect to -> https://printninja.com/calculator/status/approved/*
RewriteCond %{HTTP_HOST} ^proofs\.printingupload\.com$
RewriteRule ^approved\/(.*)$ https://printninja.com/calculator/status/approved/$1 [L]
# proofs.printingupload.com/rejected/* --redirect to -> http://staging.printninja.com/calculator/status/rejected/*
RewriteCond %{HTTP_HOST} ^proofs\.printingupload\.com$
RewriteRule ^rejected\/(.*)$ https://printninja.com/calculator/status/rejected/$1 [L]
# proofs.printingupload.com/* --redirect to -> http://staging.printninja.com/calculator/rackfile/*
RewriteCond %{HTTP_HOST} ^proofs\.printingupload\.com$
RewriteRule ^(.*)$ https://printninja.com/calculator/rackfile/$1 [L]

Have your htaccess Rules file in following manner, please make sure to clear your browser cache before testing your URLs.
# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
RewriteEngine On
#### PrintNinja unique redirects
RewriteCond %{HTTP_HOST} ^proofs\.printingupload\.com$ [NC]
RewriteRule ^(.*)/?$ https://printninja.com/calculator/rackfile/$1 [R=301,NE,L]
RewriteCond %{HTTP_HOST} ^printninja.com$ [NC]
RewriteRule ^learn/(.*) /printing-resource-center/ [NC,L,R=301]
RewriteRule ^digital-downloads/(.*) /printing-resource-center/ [NC,L,R=301]
RewriteRule ^resources/(.*) /printing-resource-center/ [NC,L,R=301]
RewriteRule ^pricing\/(.*)$ https://printninja.com/calculator/$1 [L]
#### Begin Uploader rewrites
# printingupload.com/files-submited/* --redirect to -> https://printninja.com/calculator/files-submited/*
RewriteCond %{HTTP_HOST} ^(www\.)?printingupload\.com$
RewriteRule ^printingupload\.com/files-submited\/(.*)$ https://printninja.com/calculator/files-submited/$1 [L]
# printingupload.com/invalid-order/* --redirect to -> https://printninja.com/calculator/invalid-order/*
RewriteCond %{HTTP_HOST} ^(www\.)?printingupload\.com$
RewriteRule ^printingupload\.com/invalid-order\/(.*)$ https://printninja.com/calculator/invalid-order/$1 [L]
# printingupload.com/* --redirect to -> https://printninja.com/calculator/products/upload_proofing/* (this rule must appear after the other printingupload.com/* rules)
RewriteCond %{HTTP_HOST} ^(www\.)?printingupload\.com$
RewriteRule ^printingupload\.com\/(.*)$ https://printninja.com/calculator/products/upload_proofing/$1 [L]
# submit.printingupload.com/* --redirect to -> https://printninja.com/calculator/submit_proofing/*
RewriteCond %{HTTP_HOST} ^submit\.printingupload\.com$
RewriteRule ^submit\.printingupload\.com\/(.*)$ http://printninja.com/calculator/submit_proofing/$1 [L]
# proofs.printingupload.com/approved/* --redirect to -> https://printninja.com/calculator/status/approved/*
RewriteCond %{HTTP_HOST} ^proofs\.printingupload\.com$
RewriteRule ^approved\/(.*)$ https://printninja.com/calculator/status/approved/$1 [L]
# proofs.printingupload.com/rejected/* --redirect to -> http://staging.printninja.com/calculator/status/rejected/*
RewriteCond %{HTTP_HOST} ^proofs\.printingupload\.com$
RewriteRule ^rejected\/(.*)$ https://printninja.com/calculator/status/rejected/$1 [L]
# proofs.printingupload.com/* --redirect to -> http://staging.printninja.com/calculator/rackfile/*

Related

.htaccess redirect all pages of old site to same pages on a subdirectory of a different site

I need to redirect all pages of an old site to a subdirectory of our main site. E.g.:
oldsite.com/page1
to
mainsite.com/oldsite/page1
I have tried adding the following to the top of .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldsite.com$
RewriteCond (.*)$ http://www.mainsite.com/oldsite/$1 [R=301,L]
</IfModule>
When I open a fresh anonymous window, the redirect does not work, even on the homepage. So it is probably another rule getting in the way. The following is at the bottom of the .htaccess file:
# BEGIN W3TC Page Cache core
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} =on
RewriteRule .* - [E=W3TC_SSL:_ssl]
RewriteCond %{SERVER_PORT} =443
RewriteRule .* - [E=W3TC_SSL:_ssl]
RewriteCond %{HTTP:X-Forwarded-Proto} =https [NC]
RewriteRule .* - [E=W3TC_SSL:_ssl]
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteRule .* - [E=W3TC_ENC:_gzip]
RewriteCond %{HTTP_COOKIE} w3tc_preview [NC]
RewriteRule .* - [E=W3TC_PREVIEW:_preview]
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{QUERY_STRING} =""
RewriteCond %{HTTP_COOKIE} !(comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle) [NC]
RewriteCond %{REQUEST_URI} \/$
RewriteCond "%{DOCUMENT_ROOT}/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_SSL}%{ENV:W3TC_PREVIEW}.html%{ENV:W3TC_ENC}" -f
RewriteRule .* "/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_SSL}%{ENV:W3TC_PREVIEW}.html%{ENV:W3TC_ENC}" [L]
</IfModule>
# END W3TC Page Cache core
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_ACCEPT} image/webp
RewriteCond %{REQUEST_FILENAME} (.*)\.(jpe?g|png)$
RewriteCond %{REQUEST_FILENAME}\.webp -f
RewriteCond %{QUERY_STRING} !type=original
RewriteRule (.+)\.(jpe?g|png)$ %{REQUEST_FILENAME}.webp [T=image/webp,E=accept:1,L]
</IfModule>
<IfModule mod_headers.c>
Header append Vary Accept env=REDIRECT_accept
</IfModule>
Any ideas on what I should change to achieve my desired result?:
oldsite.com/page1
to
mainsite.com/oldsite/page1
Solved!
I removed all the rewrite rules that were already in there, apart from the wordpress one.
I then used this rule instead of the one I originally posted:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^oldsite.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.oldsite.com$
RewriteRule (.*)$ http://www.mainsite.com/oldsite/$1 [R=301,L]
</IfModule>

Rewritecond by `%{HTTP_HOST}` is ignored?

I have multiple domains that point to the same folder on my server. The code in index.php page recognizes which domain is accessing it and shows different content for each domain.
Now I want that www.domain-a.com\sitemap.xml opens /sitemap-a.xml so that each domain has its own sitemap.
I use the following rule in my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*domain-a\.com$
RewriteRule ^sitemap.xml$ sitemap-a.xml [NC]
However, if I access www.domain-a.com\sitemap.xml then the content from the file /sitemap.xml is shown instead of the file sitemap-a.xml.
Is the redirecting rule wrong? If so, how can I fix it?
I am using Laravel. This is the full .htaccess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*domain-a\.com$
RewriteRule ^sitemap.xml$ sitemap-a.xml [NC]
# browser request: .php to non-php
# https://stackoverflow.com/questions/13222850/redirect-php-urls-to-urls-without-extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Can you try this rule just above # Handle Authorization Header line:
RewriteCond %{HTTP_HOST} (?:^|\.)domain-a\.com$ [NC]
RewriteRule ^sitemap\.xml$ /sitemap-a.xml [L,NC]

Mistakes in htaccess redirects?

I am a beginner with htaccess stuff and need help checking for mistakes in my htaccess codes below ?
I have 3 set of redirect codes and wondering if they interfere with each other ?
Also wondering if I have redundant redirect code.
Also specifically within the "Stop Hotlinking Code", is this RewriteCond correct?
RewriteCond %{HTTP_REFERER} !^http://(www\.)%{HTTP_HOST}/.*$ [NC]
My goal is to KEEP the www and trailing slash for my URLs , and to remove the index.html for my URLs
Here is my .htaccess File
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
#Force Trailing Slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1/ [L,R=301]
#Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1/ [R=301,L]
#Redirect index.html
RewriteCond %{THE_REQUEST} ^.*/index.html [NC]
RewriteRule ^(.*)index.html$ http://www.%{HTTP_HOST}/$1/ [R=301,L]
#Stop Hotlinking
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)%{HTTP_HOST}/.*$ [NC]
RewriteRule \.(gif|jpg|jpeg|bmp|zip|rar|mp3|flv|swf|xml|php|png|css|pdf)$ - [F]
.
Edit:
Questions:
-Is my updated htaccess below correct?
-Do I put RewriteBase / at the top or keep it as is (one with the # Begin WordPress rules and a second RewriteBase / with # Force Trailing Slash ?
-Do I need Options +FollowSymLinks and if yes where do I put this?
Notice that I:
-removed the RewriteEngine On and RewriteBase / from WordPress rules and kept it at the top
-put WordPress Rules BELOW redirects
-removed end slashes for #Redirect non-www to www and #Redirect index.html
RewriteEngine On
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Redirect index.html
RewriteCond %{THE_REQUEST} ^.*/index.html [NC]
RewriteRule ^(.*)index.html$ http://www.domain.com/$1 [R=301,L]
# Force Trailing Slash
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1/ [L,R=301]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Stop Hotlinking
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)domain.com/.*$ [NC]
RewriteRule \.(gif|jpg|jpeg|bmp|zip|rar|mp3|flv|swf|xml|php|png|css|pdf)$ - [F]
No it's not correct. You can't have % variables or back references in the regex part of the condition. You could use a regex backreference though. Try:
RewriteCond %{HTTP_HOST}:::%{HTTP_REFERER} !^([^:]+):::http://(www\.)\1/.*$ [NC]

.htaccess & mod_rewrite redirect issues

Alright. I'm having no luck. So I figured I'd see if you all could help make some sense of this issue.
I've inherited a site that previously had a lot of different subdomains, which have now all been written into the .htaccess file to rewrite as subdirectories (i.e., blog.site.com becomes site.com/blog).
To avoid duplicate content issues, I also need to rewrite all site.com URL's as www.site.com. And even though the current rules look good to me, they're apparently not rewriting all the subdirectories of the domain. (I've never been that great with RegEx, so that could be part of the problem.)
Here are the current rules from .htaccess:
#---------------------------------
# Start rewrite engine
#---------------------------------
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{PATH_INFO} !^$
RewriteCond %{HTTP_HOST} !^localhost$ [NC]
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} !^blog\..+$ [NC]
RewriteCond %{HTTP_HOST} !^docs\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
#---------------------------------
# Rewrite some of our subdomains
#---------------------------------
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^blog\.site\.com$ [NC]
RewriteRule (.*)$ "http://www.site.com/blog/$1" [R=301,QSA,L]
RewriteCond %{HTTP_HOST} ^docs\.site\.com$ [NC]
RewriteRule (.*)$ http://www.site.com/docs/$1 [R=301,QSA,L]
RewriteCond %{HTTP_HOST} ^wiki\.site\.com$ [NC]
RewriteRule (.*)$ http://www.site.com/wiki/$1 [R=301,L]
</IfModule>
#---------------------------------
# Wordpress specific - do not place anything below this line
#---------------------------------
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
(Obviously, some of them exist to allow for a local dev environment, etc).
Any thoughts on why I'm not seeing site.com/directory rewritten as www.site.com/directory? What am I missing here?

.htaccess redirection not working

I try to redirect http://mydomain.com to http://www.mydomain.com
I add this to my htaccess file, but it not work :
RewriteCond %{HTTP_HOST} ^mydomain\.fr [NC]
RewriteRule ^(.*)$ http://www.mydomain.fr/$1 [L,R=301]
This is the complete file :
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.fr [NC]
RewriteRule ^(.*)$ http://www.mydomain.fr/$1 [L,R=301]
# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /
# we skip all files with .something
#RewriteCond %{REQUEST_URI} \..+$
#RewriteCond %{REQUEST_URI} !\.html$
#RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Try:
RewriteCond %{HTTP_HOST} !^www.mydomain.fr [NC]
RewriteRule ^(.*)$ http://www.mydomain.fr/$1 [L,R=301]
I'm using this on an existing site at the moment - seems to work fine here.
# Never keep domain name without subdomain
RewriteCond %{HTTP_HOST} ^mydomain\.fr$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.fr/$1 [R=301,L]

Resources