htcaccess external redirect is not excluding URI - .htaccess

I'm trying to exclude few URI from htaccess based redirection
My htaccess directives are bellow
<IfModule mod_rewrite.c>
RewriteEngine on
#
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com$ [NC]
RewriteRule !=/test$ http://domain2.com [L,R=301,NC]
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
What wrong in this? Any suggesion?
I have tried in a different way as well
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !=/test
RewriteRule (.*) http://domain2.com? [R=301,L]
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
While doing external redirection, exclusion of the the URI, test is not in effect. If I'm doing an internal rewrite, its working fine.

Maybe this works:
Options +FollowSymlinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !/test
# Add next line
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule (.*) http://domain2.com? [R=301,L]
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

Related

Htaccess Issue - Http Pages redirect to Home index.php

I have pages with redirecting to www.homage.com/index.php.
It should be redirecting to the www version. here my Htaccess File:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
# Redirect non-www urls to www
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mindscopeproducts\.com
RewriteRule (.*) http://www.mindscopeproducts.com/$1 [R=301,L]
Have it this way:
Options +FollowSymLinks
RewriteEngine on
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect non-www urls to www
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.mindscopeproducts.com%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1 [L]
i.e. keep external redirect rule before other forwarding rules. Make sure to clear your browser cache when testing this.
Reorder your rules like this :
<IfModule mod_rewrite.c>
RewriteEngine on
Options +FollowSymLinks
#Redirect non-www urls to www
RewriteCond %{HTTP_HOST} !^www\.mindscopeproducts\.com$
RewriteRule ^(.*)$ http://www.mindscopeproducts.com/$1 [R,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Removing index.php from subdomain

I have the following .htaccess file in my subdomain.
My website has http://hello/forum/index.php, and would like to hide this index.php file, and make it http://hello/forum.
Whenever I use the attached .htaccess, it directs to http://hello, not http://hello/forum. Am I missing something?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) forum/$1$2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ forum/index.php/$1 [L]
</IfModule>
Try these rules:
RewriteCond %{THE_REQUEST} ^GET\ /(forum)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /forum/index.php/$1 [L]
I was able to solve this issue by adding
DirectoryIndex index.php index.html
in .htaccess file.

Why are clean URLs not working when redirecting to https?

I added some lines in my htacess file to redirect all http requests to https. Now when I type in a URL like: http://example.com/frames/view/4701362, it redirects to: https://example.com/index.php?q=frames/view/4701362. I can go to the page manually: https://example.com/frames/view/4701362 and the URL does not get changed.
I have Clean URLs set up
Htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
#some more stuff here, unrelated
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>
Edit:
I tried reversing the Drupal index.php?q= line and the https lines like this...
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
But then my site did not load properly and said Page not found even at the homepage.
Thanks to #MikeRockett above, I was able to figure this out. In addition to his suggestion, I had to add [L,R=301] to the RewriteRule line.
<IfModule mod_rewrite.c>
RewriteEngine on
#some more stuff here, unrelated
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

remove index.php from url only for the client

I know there are some questions like this out there but the solutions are not helpful for me.
I´m using htaccess to redirect to the index.php if the request file isn´t defined (code above).
I´d like to add a code-line that if the user requests for example:
http://www.example.com/test/index.php
he will redirect to
http://www.example.com/test/
but internal it will redirect to the index.php.
Is there a way to extend my code so that it works like I want it to?
# Add Slash
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
# Redirect to index.php if the following sites were not match
RewriteBase /
RewriteCond %{REQUEST_URI} \.(php|sql|tgz|gz|txt|ttf|TTF|log|txt|ini|html|xml|xhtml|htm)$
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^/phpinfo.php$
RewriteCond %{REQUEST_URI} !^/out.php$
RewriteCond %{REQUEST_URI} !^/sitemap[^0-9]*\.xml$
RewriteCond %{REQUEST_URI} !^/robots.txt$
RewriteRule ^(.*)$ /index.php?dir=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ /index.php?dir=$1 [QSA,L]
Complete code:
DirectoryIndex index.php index.html
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# Take off index.php
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.(php|sql|tgz|gz|txt|ttf|TTF|log|txt|ini|html|xml|xhtml|htm)$
RewriteCond %{REQUEST_URI} !^/((index|phpinfo|out)\.php|robots\.txt|sitemap[^0-9]*\.xml)$
RewriteRule ^(.*)$ /index.php?dir=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?dir=$1 [QSA,L]
# Add Slash for non-files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

Need modrewrite of wildcard subdomain

I am trying to add a "/login" to wildcard subdomains.
For example:
*.example.com would be redirected to *.example.com/login
but, i want certain subdomains not to be redirected, such as...
help.example.com, m.example.com, etc.
What is the best way to do this, and would this be done in .htaccess or in httpd.conf?
Thank you for your help.
Updated below:
Here is what I currently have:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^m.example.com$
RewriteCond %{HTTP_HOST} ^(.+).example.com$ [NC]
RewriteRule ^/$ https://%1/login [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
You were almost there
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^m\.example\.com$
RewriteCond %{HTTP_HOST} !^help\.example\.com$
RewriteCond %{HTTP_HOST} ^(.+\.example\.com)$ [NC]
RewriteRule ^/?$ https://%1/login [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Resources