htaccess rewrite from a php file to subdomain - apache

I would like to redirect from example.com/profile.php?UserName=xxxx to xxxx.example.com
xxxx contains a-z or 0-9.
I have tried bunch of codes to do it (some from this site) but none of them worked as I wanted it to be. Here is my current code:
RewriteEngine on
#this should redirect example.com/profile.php?UserName=x to x.site.com
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC]
RewriteRule (.*) %1/$1 [QSA,L]
#if link does not contain subdomain add www
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

You need to put all of your redirect rules before your internal routing rules. The rules with the R flag redirect.
RewriteEngine on
# This redirects the browser to include the "www"
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# This redirects the browser when profile.php is directly accessed
RewriteCond %{THE_REQUEST} \ /+profile\.php\?UserName=([a-z0-9-_]+)
RewriteRule ^ http://%1.example.com/? [L,R]
# this internally routes the XXX.example.com to the profile.php script
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-_]+).example.com [NC]
RewriteRule ^$ /profile.php?UserName=%1 [QSA,L]

Since there was not a good answer here.. In order to rename a php file and use the subdomain extention.. I had to edit my DNS settings.. This tutorial helped :http://www.mediacollege.com/internet/server/apache/mod-rewrite/subdomains.html

Related

Force www and redirect root directory

I couldn't find a question that answered mine, or maybe I couldn't search using the right terms, but come on. I have the following rule in my htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com\.br$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\.br$
RewriteRule ^(.*)$ "https\:\/\/www\.domain\.com\.br\/site" [R=301,L]
This works when the user enters only the URL (domain.com.br or www.domain.com.br), but I need it to redirect when the user accesses this way:
domain.com.br or www.domain.com.br --> https://www.domain.com.br/site
domain.com.br/XXX --> https://www.domain.com.br/XXX
What rule should I use for this?
Update: the server already has a default rule to force SSL, in which case it is unnecessary to put it in htaccess
Rule update:
**On virtual host:**
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
**On htaccess file:**
RewriteCond %{HTTP_HOST} ^domain\.com\.br$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\.br$
RewriteRule ^/?$ https://www.domain.com.br/site/ [R=301,L]
Your code generates a redirect loop. Also, you don't need to escape slashes on targets.
You could merge everything inside your virtual host block (faster than using a htaccess):
RewriteEngine On
# force https
RewriteCond %{HTTPS} off [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# force www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# redirect root directory to /site/
RewriteRule ^/?$ /site/ [R=301,L]
Of course, you will see a redirect chain for e.g. http://domain.tld/ as it will first redirect to https://domain.tld/ then to https://www.domain.tld/ and finally to https://www.domain/tld/site/. This is fine. However, if you really want to handle everything with only one redirect, you could. But, it will be less generic.
For instance, you would end up with those rules:
RewriteEngine On
# force root directory to /site/ (https+www)
RewriteRule ^/?$ https://www.domain.com.br/site/ [R=301,L]
# force any other pages to https+www if not the case already
RewriteCond %{HTTPS} off [NC,OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.domain.com.br%{REQUEST_URI} [R=301,L]

How to reduce the number of 301 redirect?

For my website I want to force an URL with https, without www and without .php extension.
For that my htaccess file contains the following rules:
RewriteEngine On
#force HTTPS and remove the www
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} ^www\.jvincent\.fr$ [NC]
RewriteRule ^(.*) https://jvincent.fr/$1 [QSA,L,R=301]
#To remove the .php extension
RewriteCond %{REQUEST_URI} !(php-script) [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]
It works fine.
But if I access to my website using the URL http://www.jvincent.fr/phpinfo.php I have two 301 redirects :
http://www.jvincent.fr/phpinfo.php to
https://jvincent.fr/phpinfo.php
https://jvincent.fr/phpinfo.php to https://jvincent.fr/phpinfo
I searched but I was not able to find a solution to optimize and have only one 301 redirect in all cases.
I there a way to do that? Merging all the conditions before an unique RewriteRule? Using environment variable (flag E in the first part)?
Thanks for your help.
Jack
You can use:
RewriteEngine On
#force HTTPS and remove the www
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} ^www\.jvincent\.fr$ [NC]
RewriteRule ^(.*?)(?:\.php)?$ https://jvincent.fr/$1 [NC,QSA,L,R=301]
#To remove the .php extension
RewriteCond %{REQUEST_URI} !(php-script) [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ (.*)\.php [NC]
RewriteRule ^ %1 [R=301,L]

Properly redirecting a subdomain to subdirectory invisibly

I know similar things have been asked before, but none of the solutions I've found have seemed to work out. I'm by far an expert when it comes to mod_rewrite and it's ilk, so apologies if I'm missing something obvious.
I am trying to invisibly redirect subdomains to an index.php file in a subdirectory; this file takes the value of the subdomain as part of the query string, which is working fine.
The problem I'm having is that now everything in this subdirectory is being redirected to the index.php file, which I don't want to happen.
This is what I have thus far:
RewriteEngine On
RewriteBase /
# User dashboards
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule ^.*$ app/index.php?user=%1 [L,NC,QSA]
What I'm looking for is a situation where http://subdomain.example.com/ will lead to /app/index.php?user=subdomain, but http://subdomain.example.com/assets/stylesheet.css will go to /app/assets/stylesheet.css.
Thanks in advance!
If i understood well your example, you can do it this way:
redirects example.com to www.example.com to avoid having "empty" subdomain
internally rewrites every root subdomains (except www) to /app/index.php?user=subdomain
internally rewrites other things with "app" prefix
Which is represented by this code
RewriteEngine on
# redirects example.com to www.example.com to avoid having "empty" subdomain
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# internally rewrites every root subdomains (except www) to /app/index.php?user=subdomain
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\. [NC]
RewriteRule ^/?$ /app/index.php?user=%1 [L,NC,QSA]
# internally rewrites other things with "app" prefix
RewriteCond %{THE_REQUEST} !app/
RewriteRule ^/?(.+)$ /app/$1 [L,NC,QSA]
EDIT: as you asked in below comments, here is how to manage also www subdomain
RewriteEngine on
# redirects example.com to www.example.com to avoid having "empty" subdomain
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# internally redirects www subdomain root to /site/index.php
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^/?$ /site/index.php [L]
# internally rewrites every other root subdomains to /app/index.php?user=subdomain
RewriteCond %{HTTP_HOST} ^([^.]+)\. [NC]
RewriteRule ^/?$ /app/index.php?user=%1 [L,NC,QSA]
# internally rewrites other things with "app" prefix
RewriteCond %{THE_REQUEST} !app/
RewriteRule ^/?(.+)$ /app/$1 [L,NC,QSA]
Let add second rule to redirect assets to app/assets:
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|gif)$ [NC]
RewriteRule ^.*$ app/index.php?user=%1 [L,QSA]
RewriteRule ^assets/(.*)$ app/assets/$1 [L,QSA]
or load all css/js/images directly from app:
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule ^.*\.(css|js|png|jpg|gif)$ app/$0 [NC, QSA]
RewriteRule ^.*$ app/index.php?user=%1 [L,QSA]
EDIT: Sorry, I haven't tested it before, so there is working example:
RewriteRule ^assets/(.*)$ app/assets/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule !^app/assets/ app/index.php?user=%1 [L,QSA]

Redirect subdomain to main domain

I'm dealing with a site that supports both English and Spanish, and I'd like to know how to redirect all the requests to the language-specific subdomains to the main domain (en.mysite.com and es.mysite.com to mysite.com).
The whole site is programmed in PHP and it has a main script index.php that processes the language and section GET parameters and displays stuff accordingly.
Now, I tried to do the following in the .htaccess file in the root of mysite.com. I think it clarifies what I'm trying to do:
RewriteEngine On
# English redirects
RewriteRule ^en.mysite.com$ index.php?language=English&section=Main
RewriteRule ^en.mysite.com/store$ index.php?language=English&section=Store
RewriteRule ^en.mysite.com/create_account$ index.php?language=English&section=CreateAcc
# Spanish redirects
RewriteRule ^es.mysite.com$ index.php?language=Spanish&section=Feed
RewriteRule ^es.mysite.com/comprar$ index.php?language=Spanish&section=Store
RewriteRule ^es.mysite.com/crear_cuenta$ index.php?language=Spanish&section=CreateAcc
But this doesn't work. Can anyone tell me what I'm doing wrong here?
You can't include the hostname as part of the match in a RewriteRule, as it only matches against the URI. You'll need to use a separate RewriteCond that matches against the hostname for each of your rules:
RewriteCond %{HTTP_HOST} ^en.mysite.com$ [NC]
RewriteRule ^$ /index.php?language=English&section=Main
RewriteCond %{HTTP_HOST} ^en.mysite.com$ [NC]
RewriteRule ^store$ /index.php?language=English&section=Store
RewriteCond %{HTTP_HOST} ^en.mysite.com$ [NC]
RewriteRule ^create_account$ /index.php?language=English&section=CreateAcc
Then the spanish rewrites:
RewriteCond %{HTTP_HOST} ^es.mysite.com$ [NC]
RewriteRule ^$ /index.php?language=English&section=Feed
RewriteCond %{HTTP_HOST} ^es.mysite.com$ [NC]
RewriteRule ^comprar$ /index.php?language=English&section=Store
RewriteCond %{HTTP_HOST} ^es.mysite.com$ [NC]
RewriteRule ^crear_cuenta$ /index.php?language=English&section=CreateAcc

Some Problem with htaccess!

How can I redirect my users from example.com / or www.example.com to new.example.com
but I dont want to redirect some specific urls like:
www.example.com/test.php
api.example.com/testo.php
www.example.com/forum/index.php
I did:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)$ http://new.example.com/$1 [R=301,L]
but what is the rules for urls?
One way to do this is to create additional rules earlier in the .htaccess file for the specific URLs that just redirect to themselves (internally, not using a 3XX response), and then use the L flag so no later rules are processed for those URLs.
For example:
RewriteEngine on
# don't redirect these
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(/test.php)$ \1 [L]
RewriteCond %{HTTP_HOST} ^api.example.com$
RewriteRule ^(/testo.php)$ \1 [L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(/forum/index.php)$ \1 [L]
# redirect these
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^(.*)$ http://new.example.com/$1 [R=301,L]
I'm not sure if your "don't redirect" requirements included the hostname. If not, then just remove the RewriteCond lines.