Htaccess redirect Laravel 4 - regex

I have a Laravel 4 installation, and I used the boilerplate's htaccess to redirect my site from:
site.com.br to www.site.com.br (notice the www).
But when I use a route such as site.com.br/TITLE-OF-MY-POST I am redirected to:
www.site.com.br/index.php instead of www.site.com.br/TITLE-OF-MY-POST"
Does anyone know why? Here my htaccess rules:
Laravel's rule
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Boilerplate's rule
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>

Change order of your rules:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
In general keep your 301 rules before your internal rewrite rules.

Related

Redirect all wild card subdomains and subdomain urls to the root domain and root domain urls using htaccess

I'm trying to redirect my subdmain in this manner: anysubdomain.my_domain.com and its URLs must be redirected to root domain and its URLs respectively.
For example (1) if a user tries http://anysubdomain.my_domain.com it should redirect tohttps://www.my_domain.com.
And (2) if a user tries http://anysubdomain.my_domain.com/anyurl it should be redirected to https://www.my_domain.com/anyurl.
I was able to achieve (1) using below htaccess code:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} !^www.my_domain.com$
RewriteRule ^(.*)$ https://www.my_domain.com/$1 [QSA,L,R=301]
</IfModule>
yet I could not do the (2) part. I'm sure we can do this with a modification for above htaccess code, but I don't know how.
I already have wildcard cname, that is why (1) perfectly working.
Also it is a WordPress site, if that matters.
Put these rules first :
RewriteCond %{HTTP_HOST} !^www.my_domain.com$
RewriteRule ^(.*)$ https://www.my_domain.com/$1 [QSA,L,R=301]
like this :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?my_domain\.com$
RewriteRule ^(.*)$ https://www.my_domain.com/$1 [QSA,L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Note: clear browser cache the test.
You may use this .htaccess to handle all subdomains:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} (?:^|\.)(my_domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
Make sure to test it from new browser to clear browser cache fully.

HTACCESS Treat important directory as a parameter

I want directory www.example.com/core to be translated into string, instead of just dissalowing access to it. is that possible?
UPDATE (STILL NO LUCK):
My current .htaccess
#Options -Multiviews
RewriteEngine On
#Remove the comments below to enable enforcing HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Handle Front Controller...
RewriteBase /public
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ launcher.php?urls=$1 [QSA,L]
RewriteRule . launcher.php [L]
Here are some scenarios:
I write a url www.example.com/some-dir-that-does-not-exist and it works fine.
I write a url www.example.com/url-that-DOES-exist and the browser redirects it to www.example.com/url-that-DOES-exist/?url=url-that-DOES-exist
That is due to mod_dir module adding a trailing slash in front of real directories and making a 301 redirect after your rewrite rule.
To fix have it like this:
DirectorySlash Off
RewriteEngine On
RewriteBase /public/
RewriteCond %{THE_REQUEST} /launcher\.php [NC]
RewriteRule ^ - [F]
#Remove the comments below to enable enforcing HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# add a trailing slash to directories
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ launcher.php?urls=$1 [QSA,L]
Your code will become:
#Options -Multiviews
RewriteEngine On
#Remove the comments below to enable enforcing HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /public
# Treat existing /core directory as non-existing (handled by launcher.php)
RewriteCond %{REQUEST_URI} ^/core(/.*)?$
RewriteRule . launcher.php [L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ launcher.php?urls=$1 [QSA,L]
RewriteRule . launcher.php [L]

Redirect single page to HTTPS and the rest to HTTP

I'm trying to redirect a single WordPress page (/scheduling/) to HTTPS, while forcing HTTP on all of the other pages. There are a lot of similar posts on this topic, but my specific use case isn't working for some reason. Thoughts?
.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} ^scheduling/
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^scheduling/
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The above is properly redirecting all other pages to be redirected to http whenever https is requested, but isn't doing anything at all for the scheduling/ page.
Do redirects before default WP rule.
Use THE_REQUEST variable instead of REQUEST_URI.
You can use:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} /scheduling [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !/scheduling [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

htaccess URL rewrite - requested URL not found on server

Need to hide one folder while loading like Uniimart1609/S/page.php to Uniimart1609/page.php
in linux
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule Home_Page.php$ S/Home_Page.php
</IfModule>
is redirected http://Uniimart1609/Home_Page.php this is working fine, but when i rewrite the htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /S/$1.php [L,NC,QSA]
</IfModule>
getting The requested URL /S/Home_Page.php was not found on this server. Pleas help
Have your rule like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /Uniimart1609/
RewriteCond %{THE_REQUEST} /S/(\S*) [NC]
RewriteRule ^ %1? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/S/$1\.php -f [NC]
RewriteRule ^([^./]+)/?$ S/$1.php [L]
</IfModule>

.htaccess force https but exclude a subdomain

How would I exclude a subdomain from getting forced to https and redirected to main url? I'm using the below in my .htaccess file but need a little help as my subdomain (stage.mydomain.com) gets redirected / can't access.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I've tried a few options but no luck - Any ideas? Thanks!
Have your first rule like this:
RewriteCond %{HTTP_HOST} !^stage\.mydomain\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]