How can htaccess replace www for the current directory name - .htaccess

First
Is it possible to stablish a rewrite rule so that when I type in the url bar
blog.mywebsite.com/file
it redirects to
www.mywebsite.com/blog/file.php?
The same if i wanted to go to the store directory of the site. Could I have a rule to redirect
store.mywebsite.com/file
to
www.mywebsite.com/store/file.php?
Second
Provided first part is ok, how could I do it to redirect this
blog.mywebsite.com/file/this-is-my-article
to
www.mywebsite.com/blog/file.php?url=this-is-my-article

You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^[^.]+\.mywebsite\.com$ [NC]
RewriteRule ^/?$ /%1/file.php? [L,R=302]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^[^.]+\.mywebsite\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ /%1/file.php?url=$1 [QSA,L,R=302]

Related

.Htaccess redirect breaks file path

I'm attempting to canonicalization my URL. However, when adding the relevant code, it breaks my other rewrite rules which allows me to hide the .html/ .php. It works fine when landing on the homepage but when you go to a page with www. and a file path at the end of the URL the page redirects to the homepage without www.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
RewriteCond %{HTTP_HOST} ^www\.bespoke-apertures\.co.uk$ [NC]
RewriteRule (.*) https://bespoke-apertures.co.uk/$1 [R=301,L]
Have it like this in different order:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(bespoke-apertures\.co.uk)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA]
Make sure to test this in a new browser to avoid old cache.
Try this to redirect without www :
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

How to write a htaccess file to directory from wildcard subdomain

I am creating dynamic (wildcard) sub-domains using .htaccess like so:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+) /index.php?n=%1&v=$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com/why/$ [NC]
RewriteRule ^(.+)$ /$1/ [QSA,L]
When a directory is included in the URI, e.g. dynamic.domain.com/directory the URL changes to the "real" subdomain like so: subdomain.domain.com/directory/
My question is how the keep the URL from showing the actual subdomain.
xyz.domain.com works as expected
xyz.domain.com/dir will change the URL to dynamic.domain.com/dir which should be xyz.domain.com/dir

Redirect www to https://www in .htaccess

So far I have .htaccess file which is able to redirect correctly from (domain.com) to (https://www.domain.com) but not working if I go to www.domain.com and it must redirect (www.domain.com) to (https://www.domain.com)
I need (https://www) always before my site url
my .htaccess file
RewriteEngine On
RewriteCond !{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Replace your first 2 redirect rules with this rule:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
Make sure to clear your browser cache before testing this.

.htaccess redirect only redirects after the first link is clicked

I have an .htaccess file that is supposed to be redirecting to several different domains/subdirectories:
RewriteEngine on
#this works
RewriteCond %{HTTP_HOST} ^(www\.)?domainA\.ca$ [NC]
RewriteCond %{DOCUMENT_ROOT}/foundation%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
#this works
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?domainB\.ca$ [NC]
RewriteRule ^(.*)$ /foundation/$1 [QSA,L]
#this sort of works
RewriteCond %{HTTP_HOST} .
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.+)$ index.php?/$1 [QSA]
RewriteRule (.*) http://www.domainC.ca/index.php?/$1 [R=301,QSA]
They have several more domains set up as aliases on the account domainD, domainE and so on. If someone requests domainD then the browser's address bar will say on domainD = no rewrite, as soon as they click a link they will get forwarded to domainC/whatever-link/ which is correct, but what am I missing to get the inital domainD -> domainC rewrite to work?
You can try these rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domainC\.ca$ [NC]
RewriteRule ^ http://www.domainC.ca%{REQUEST_URI} [R=301,L,NE]

website proper redirecting via .htaccess

I searched about .htaccess redirecting address after first symbol / but I can't get it working.
.htaccess file is like this one:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /index.php?route=/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^OldDomain [NC]
RewriteRule ^blog/(.*)$ NewDomain/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.OldDomain [NC]
RewriteRule ^blog/(.*)$ http://www.NewDomain/$1 [R=301,L]
When I test it with the domain
www.OldDomain.com/catalog
its redirects to
www.NewDomain.eucatalog
which does not have the slash symbol "/".
How can I make the redirect keep the slash symbol?
This may be unrelated, but you need your redirect rules (the last 2) to come before your routing rule. It's possible your router (index.php) is redirecting incorrectly:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^OldDomain [NC]
RewriteRule ^blog/(.*)$ NewDomain/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.OldDomain [NC]
RewriteRule ^blog/(.*)$ http://www.NewDomain/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /index.php?route=/$1 [L,QSA]

Resources