URL rewriting for sub-domains - regex

I have a couple of sub-domains in addition to my main domain. Like so: domain.com, about.domain.com and new.domain.com.
All 3 have their own .htaccess file in their individual root folders.
I am trying to do 2 things:
all traffic gets routed to the same URL respectively.
the URL thankyou.php gets rewritten to /thank-you
Both of these work for the main domain, domain.com.
But they do not work for the sub-domains.
Here is what works for domain.com:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index.php$ http://www.domain.com [R=301,L]
RewriteRule ^thank-you/?$ thankyou.php [NC,L]
Appreciate any help with this.

Use this .htaccess for your sub domain:
Options +FollowSymLinks
RewriteEngine on
#RewriteCond %{HTTP_HOST} !^www.
#RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L,NE]
RewriteCond %{THE_REQUEST} \s/+index\.php\s
RewriteRule ^index.php$ / [R=301,L]
RewriteRule ^thank-you/?$ /thankyou.php [NC,L]

Did you try this yet?
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule (about)(/.*|$) $1.domain.com/$2 [R=301,L]

Related

htaccess remove segment from url and redirect to https and www

I am deploying a Craft CMS site to a shared hosting account using Deployer.
The latest deployment is accessible from domain.com/current/public
My .htaccess file looks like the following, which strips current/public from the url and forces https:
RewriteEngine on
RewriteRule ^(.*)$ current/public/$1
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
I now need to also redirect all urls to use www
How can I adjust my .htaccess to force www on all urls?
*** UPDATE ***
I have managed to solve the above with the following:
RewriteEngine on
RewriteRule ^(.*)$ current/public/$1
RewriteCond %{HTTP:X-Forwarded-Proto} !=https [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+) [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
When I go to example.com/admin it redirects to example.com/current/public/admin. How can I adjust my htaccess file to remove 'current/public' from admin urls?
Your http->https and www adding rule should be top most rule so that it works on original URI instead of a rewritten URI due to other rule.
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !=https [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+) [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(.*)$ current/public/$1 [L]
Make sure to clear browser cache before testing this rule.
Also add this redirect rule in /current/public/.htaccess:
RewriteCond %{THE_REQUEST} /current/public/(\S+) [NC]
RewriteRule ^ /%1 [R=301,L,NE]

Trouble removing www from url with https

I want to remove www from URL but,
I am having an issue removing www from url with https (Apache Server).
I have added the following code in .htaccess file, it works, but when I go to http://www.zubizi.com it doesn't work, what I need to do for that????
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://zubizi.com/$1 [R=301,L]
Just make the following changes:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://zubizi.com/$1 [R=301,L]
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

apache .htaccess ssl to only one site

I have an .htaccess file for some domains sitting on same folder but leads to different content that is determined by php according to the domain name.
I want only one of them to redirect all requests to ssl domain.
How that's can be done?
When I use redirect rules it transfer all domains:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%mydomain.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%mydomain.com%{REQUEST_URI} [L,R=301]
If you want to skip mydomain.com from this http->https redirection:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?mydomain\.com$
RewriteCond %{HTTPS} off
RewriteRule ^ https://%mydomain.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%mydomain.com%{REQUEST_URI} [L,R=301]

htaccess 301 redirect issues

Problem: I just did a major update to my companies website and trying to get some redirects to work. Right now if I have
Redirect 301 folder1/oldFile1.html http://www.mysite.com/newFolder1/newFolder2/newFile.html
and when you got to the new set up, I get this instead with a 404
http://www.mysite.com/newFolder1/newFolder2/newFile.htmloldFile.html
any ideas what is causing this?
Note: this only appears to happen to files that are moving from a 1 folder structure to a 2 folder structure.
RewriteOptions inherit
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mysite.com [nc]
rewriterule ^(.*)$ http://www.mysite.com/$1 [r=301,nc]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html?\ HTTP/
RewriteRule ^(.*)index\.html?$ http://www.mysite.com/$1 [R=301,L]
Don't mix mod_rewrite rules with mod_alias rules.
Use this instead:
RewriteOptions inherit
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,NC,L]
RewriteCond %{THE_REQUEST} \s/.*index\.html?\ HTTP/
RewriteRule ^(.*)index\.html?$ http://www.mysite.com/$1 [R=301,L]
RewriteRule ^folder1/oldFile1\.html$ /newFolder1/newFolder2/newFile.html? [R=301,L]

Dynamic 301 Redirect

We made a mistake in the scope of our dynamic pages on a new site, and have some incorrect pages already spidered in Google.
I need to redirect the following format:
http://www.domain.com/dir/dir/?q=120
To this format:
http://www.domain.com/dir/dir/?p=120
Only difference is the 'q' needs to be a 'p'.
RewriteEngine is on, as I've already consolidated traffic from domain.com to www.domain.com
This is what I have in my root .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^centerline.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteRule ^(/leadership/detail/)\?q=([0-9]+)$ $1?p=$2 [R=301, L]
Try this:
RewriteCond %{QUERY_STRING} q=([0-9]+)$
RewriteRule ^(.*)$ /$1?p=%1 [L,R=301]
You'll need the RewriteCond as apache doesn't allow matching against the querystring in a RewriteRule
EDIT
RewriteCond %{REQUEST_URI} ^/leadership/detail/$
RewriteCond %{QUERY_STRING} q=([0-9]+)$
RewriteRule ^(.*)$ /$1?p=%1 [L,R=301]