Using .htaccess to rewrite dynamic subdomains - php

I'm currently using .htaccess to rewrite on my website to create dynamic subdomains. I also use it to remove the .php extension on all pages. However, once inside the subdomain, it tries to redirect again. For example, if you went to https://admin.example.com/test, it would actually be accessing https://example.com/clients/admin/test.php. I keeping getting various 404 errors using the following .htaccess file:
Options +MultiViews
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^subdomains/(.*)/(.*) http://$1.example.com/$2 [r=301,nc]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ clients/%2/$1 [QSA,L]
How can I keep this from redirecting to https://admin.example.com/clients/admin/test.php?

First, you probably want to turn off multiviews, that's going to mess with the whole "removing the php extension" thing
Than, you want this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
To be after your redirects, and you want to be doing the redirect status check on the actual redirects:
Options -Multiviews + FollowSymLinks
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^subdomains/(.*)/(.*) http://$1.example.com/$2 [r=301,nc]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_URI} !^/clients/
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ clients/%2/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Related

how to combine 2 .htaccess rewrites

I have 2 different rewrites in my .htaccess file that I need to combine into 1.
The first redirects all requests to the https://www version of the website.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^w3studios\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com$
RewriteRule ^(.*)$ "https\:\/\/www\.mywebsite\.com\/$1" [R=301,L]
</IfModule>
The second gets all the post data and assigns to a variable.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
How can I combine these to both work?
Thank-you
You can use this combined set of rules in your site:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

REWRITE URL and keep QUERY STRING in .htaccess

Here is my .htaccess code...
ErrorDocument 404 /404.html
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !\.php$
RewriteCond %{REQUEST_URI} !\.css$
RewriteCond %{REQUEST_URI} !\.js$
RewriteCond %{REQUEST_URI} !\.png$
RewriteCond %{REQUEST_URI} !\.mp3$
RewriteCond %{REQUEST_URI} !\.jpg$
RewriteCond %{REQUEST_URI} !\.html$
RewriteCond %{REQUEST_URI} !\.xml$
RewriteCond %{REQUEST_URI} !\.txt$
RewriteCond %{REQUEST_URI} !\.gif$
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/example\.com" [R=301,L]
I want to have: example.com/buy?r=abc123 appear like: example.com/release/abc123 or even like: example.com/abc123
I cannot $_GET the contents of r in buy.php once the url has been redirected.
I hope someone can help :)
If you want clean URLs then you can simply use:
RewriteRule ^([^/]*)$ /buy?r=$1 [L]
to get example.com/abc123
Or you can use:
RewriteRule ^release/([^/]*)$ /buy?r=$1 [L]
to get example.com/release/abc123.
Just make sure you clear your cache before testing this.

How to set up Apache mod_rewrite redirect rules?

I am not quite familiar with Apache settings. I need to make website loading sub-directory content except one page.
Currently got a website and need to make all calls to http://www.domain.com & http://domain.com load contents from http://www.domain.com/subfolder (but looks like http://www.domain.com)
Only except the http://www.domain.com/checkout page, this one page should redirect to https://www.domain.com/checkout for secure checkout
The current mod_rewrite shown as below:
RewriteEngine on
RewriteRule ^$ domain/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/domain%{REQUEST_URI} -f
RewriteRule .* domain/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* domain/index.php?q=$0 [QSA]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://domain.com.au/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://domain.com.au$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain.com.au/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.domain.com.au$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ http://www.domain.com.au [R,NC]
Open the file named .htaccess in the root of your webserver and add following lines of code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^checkout/(.*)$ https://www.yourdomain.com/checkout/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_URI} !^/checkout/
RewriteRule ^(.*)$ /subfolder/$1 [NE,L,QSA]
rewrite for your complete .htaccess-file (check if this works, then I'll delete the previous code):
RewriteRule ^$ subfolder/index.php [QSA,L]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ http://www.yourdomain.com [NC]
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^checkout/(.*)$ https://www.yourdomain.com/checkout/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^subfolder/(.*) /subfolder/index.php?q=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{REQUEST_URI} !^/checkout/
RewriteRule ^(.*)$ /subfolder/$1 [NE,L,QSA]

.htaccess file not working after adding custom url

I am trying to just update my .htaccess file so that I get nicer looking url's (without the file extenstion of .php). The file already has a redirect in it as I have both domain names.
Here is the original code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^darrenmorton\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.darrenmorton\.com$
RewriteRule ^/?$ "http\:\/\/darrenmorton\.co\.uk\/" [R=301,L]
And here is my updated code that is not working
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^darrenmorton\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.darrenmorton\.com$
RewriteRule ^/?$ "http\:\/\/darrenmorton\.co\.uk\/" [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
Not
RewriteCond %{REQUEST_FILENAME} \.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^contact-darren-morton.php$ contact-darren-morton.php [L]
Not sure what is the problem as I am new to .htaccess files!
You can use:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?darrenmorton\.com$ [NC]
RewriteRule ^ http://darrenmorton.co.uk}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
You are redirecting everything with that statement:
RewriteRule ^/?$ "http\:\/\/darrenmorton\.co\.uk\/" [R=301,L]
Nothing after that will ever work, because it is the last rule to be checked if it matches (and it matches always). Defined by you with the [L].
So at least you need to change the order of your rules. If you want the rules to only work on your .co.uk domain you have other options, too.

htaccess not working for www and non-www

i'm trying to redirect www.site.ru and site.ru to www.site.ru/ru_RU. But i can't make it work.
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.ru$ [NC]
RewriteRule ^(.*)$ http://www.site.ru/ru_RU [L,R]
RewriteCond %{HTTP_HOST} ^www\.site\.ru$ [NC]
RewriteRule ^(.*)$ http://www.site.ru/ru_RU [L,R]
RewriteCond %{REQUEST_URI} ^/news
RewriteRule (.*) /news [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
It's not redirecting WWW version. Can someone tell me how to make that request. By the way, sometimes i come to situation, where in firefox it's working, but in IE it's not.
Try this code:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?site\.ru$ [NC]
RewriteRule ^$ http://www.site.ru/ru_RU [L,R]
## WHAT IS THIS RULE DOING??
# RewriteCond %{REQUEST_URI} ^/news
# RewriteRule ^ /news [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
</IfModule>
btw I had to comment out suspicious looking 2nd rule for /news. If you can explain what you're trying to do with this I can suggest you how to fix it.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ru_RU [R=permanent,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/ru_RU [R=301,L]

Categories