Some Problem with htaccess! - apache

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.

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]

.htaccess rewrite for https and hidden subfolder

I need to rewrite http://www.example.com and http://example.com to
https://www.example.com.
An additional complexity is that these URLs are not pointing to the index file, but to the /folder. i.e. https://www.example.com really points to /var/www/html/folder, without showing https://www.example.com/folder in the URL bar.
I had previously managed to attain the second part with
RewriteCond %{HTTP_HOST} www.example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ folder/$1 [L]
and I know that the first part can be attained with something like this:
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
but somehow I just can't get both of them to work together because I'm not familiar with htaccess. Thanks in advance!
You can have a single rule to add www and http -> https redirection and then have your rewrite rule for folder forward:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^(?!folder/)(.*)$ folder/$1 [L,NC]

htaccess rewrite from a php file to subdomain

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

mod_rewrite rules for my site

I have these rules on my site:
RewriteCond $1 !^(support|about-us|profile|support|features|videos|terms-of-service|about-us|signup|media|includes|modules|cgi-bin|templates|xmlrpc|language|libraries|plugins|administrator|component|images|dev|data)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([a-z0-9\-]+)/[^\ ]*\ HTTP/
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^([a-z0-9\-]+)/(.*)$ https://$1.example.com/$2 [R=301,L]
#
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+)\.example\.com
RewriteCond %1 !^www\.
RewriteRule ^(support|about-us|profile|support|features|newserver|videos|about-us|terms-of-service|signup|component|includes|media|cgi-bin|templates|xmlrpc|language|modules|libraries|plugins|administrator|images(/.*))$ https://example.com/$1 [R=301,L]
# Externally redirect all www hostnames to non-www hostnames
RewriteCond %{HTTP_HOST} ^(([a-z0-9\-]+\.)*)www\.(([a-z0-9\-]+\.)*)example\.com
RewriteRule ^(.*)$ https://%1%3example.com/$1 [R=301,L]
I added these below to redirect these types: https://example.com/firstnamelastname?mv=0 etc
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^mv=[0-4]$
RewriteRule ^([A-Za-z0-9]+)$ https://$1.example.com/? [L,R]
However I cannot get these to redirect https://example.com/firstnamelastname
If i remove the Query string from the last 3 lines then If i would click on features link on my website It would redirect to https://features.example.com which is what the rules at the top are preventing.
Can anyone assist with this.

Apache .hatccess rewrites not working for legacy URLS

I'm trying to rewrite some legacy Joomla URLs on a site that's now using ExpressionEngine as its CMS but they're not working.
The ExpressionEngine URL rewrites, i.e. removing index.php from the URL work fine though.
This is what I've got:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
# This is the one that's not working
RewriteRule /index.php?option=com_chronocontact&Itemid=54 /contact/ [R=301,L]
# Force www
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
# Redirect index.php Requests
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteCond %{THE_REQUEST} ^GET
RewriteRule ^index\.php(.+) $1 [R=301,L]
# Standard ExpressionEngine Rewrite
RewriteCond %{REQUEST_URI} ^/
RewriteCond %{QUERY_STRING} ^(gclid=.*)
RewriteRule ^(.+) /index.php?/ [L,PT]
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(assets|css|images|tinymce|js|min|cms|themes|index\.php|admin\.php|favicon\.ico|index\.php|path\.php|php\.ini) [NC]
RewriteRule ^(.+) /index.php?/ [L]
</IfModule>
Can anyone spot what I'm doing wrong?
The first thing is the stray RewriteCond %{HTTPS} !=on that you have at the top. It looks like it belongs to the rule under it, as in:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
As far as the rule that you have commented that doesn't work, the ? is a reserved character for regular expressions, and your pattern actually says that the second p in /index.php is "optional". Additionally, you can't match against the query string in a rewrite rule, you need to use a rewrite condition and match against the %{QUERY_STRING} variable:
RewriteCond %{QUERY_STRING} ^option=com_chronocontact&Itemid=54$
RewriteRule ^(index.php)?$ /contact/? [R=301,L]
is probably more along the lines of what you're looking for.