Consolidating .htaccess and fix bug - apache

I should start off by saying that I am no expert with .htaccess and that everything I have done with it comes from tutorials I followed.
I have edited the stock standard .htaccess file that ships with Laravel to include lines that
remove public from the URL
force https
redirect requests for www.mysite.com to mysite.com
The issue that I have currently, is that when I visit the route mysite.com/sitemap or www.mysite.com/sitemap it generates a new sitemap and then redirects to mysite.com/index.php effectively getting rid of the pretty-urls that Laravel is meant to fix.
This is my .htaccess file in my public directory:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Redirect www.mysite.com to mysite.com
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
This is the .htaccess file in my root directory
# Remove public
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
And this is my routes.php handling the sitemap route
Route::get('sitemap', function(){
// create new sitemap object
$sitemap = App::make("sitemap");
// add items to the sitemap (url, date, priority, freq)
$sitemap->add(URL::to('/'), '2015-11-14T20:10:00+02:00', '1.0', 'monthly');
$sitemap->add(URL::to('getcv'), '2015-11-14T12:30:00+02:00', '0.9', 'monthly');
// generate your sitemap (format, filename)
$sitemap->store('xml', 'sitemap');
// this will generate file sitemap.xml to your public folder
return redirect('/');
});
My question is, how can I make it so that mysite.com/sitemap or www.mysite.com still follows all of the rules in my .htaccess whilst redirect to mysite.com after following the sitemap route rather than going to mysite.com/index.php?

You need to reorder your rules in /public/.htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect www.mysite.com to mysite.com
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Make sure to clear your browser cache when you test this change.

Related

My fowards do not seem to work in .htaccess

I have an issue with an apache .htaccess redirection on a laravel site
My forwards do not seem to work
here is what I want:
Foward www to non-www www.ReplaceMe.com to ReplaceMe.com
Foward HTTP to https http://ReplaceMe.com to https://ReplaceMe.com
Foward non-trailing / to add trailing / https://ReplaceMe.com to https://ReplaceMe.com/
so the result would be.
https://www.ReplaceMe.com/about-us TO https://ReplaceMe.com/about-us/
But it does not seem to be working.
Below Code i am using now:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# www to non-www
RewriteCond %{HTTP_HOST} ^www.ReplaceMe\.com$ [NC]
RewriteRule ^(.*)$ https://ReplaceMe.com/$1 [R=301,L]
# http to https
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
</IfModule>
As far as I know, this should work. Can anyone spot my Issue?

Laravel: .htaccess rule for redirecting to https not working

I'm developing a laravel app and I need to force HTTPS for it.i've tried many other solutions that i found on the internet but none of them worked.is it possible that my app some how ignores htaccess rules?
i've already tried these solutions:
Laravel: how to force HTTPS?
here's my httaccess file :
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle redirect to Https
RewriteRule ^(.*)$ https://foody.ir/$1 [R,L]
</IfModule>

htaccess RewriteRule rewrite to subfolder

I am trying to rewrite the url 'test' to a subdirectory in /public. This is a Laravel App end I want to run the content in public/test independend of Laravel so I should be prevented to go through the Laravel router.
/test/whatever should be invisbly forwarded to /public/test/whatever (The url should stay /test/whatever).
I now have this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/test/ [NC]
RewriteRule ^(.*)$ public/test/$1 [L]
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
So far this is forwarding to: /public/test/test/(index.php)
I think I am close...
Update:
In the public directory is also a .htaccess file (Original, from Laravel 5.4):
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
You only need the last rule
RewriteRule ^(.*)$ public/$1 [L]
for the root dir .htaccess file. This will rewrite everything to the public/.htaccess, including requests starting with /test.
As long as public/test/ is a directory, and any requested URLs starting with /test/ are real files inside public/test, this will be sufficient, because the conditions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
will prevent existing files and directories to be handled by index.php.
If test is not an existing directory, or the requests for /test/... are not real files, you need additional rules. Possibly in /public/.htaccess or in /public/test/.htaccess.
The reason for /test being rewritten to /public/test/test was the combination of
# /.htaccess
RewriteRule ^(.*)$ public/test/$1 [L]
where (.*) already includes test, which becomes public/test/test, and then
# /public/.htaccess
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
because public/test/test is not an existing file or directory (-> RewriteCond), and is therefore handled by this rule.

Apache htaccess rewrite slash followed by questionmark

I'm working on a Laravel Lumen project and issue something strange. I created a restful API on some of the routes. If I call the API directly from my browser everything seems to work. However if I use an iPhone client application of a debugging interface an additional slash is added.
The API is currently located at:
http://.../public/index.php/api/fever?api&items
Whenever an iPhone application or debug tool is used the following location is requested:
http://.../public/index.php/api/fever/?api&items
This results in a 'page not found' error. Is is possible to use the apache htaccess rewrite rule to redirect all api/fever/? to api/fever? ??
The htaccess file has to be located in the public folder, which is located under a sub folder under the main website.
Thanks in advance
current htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
You can try this redirect rule at top of your site root .htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} !.*/api/fever/ [NC]
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
RewriteCond %{THE_REQUEST} \s/(.*api/fever/\S*)\s [NC]
RewriteRule ^ /%1 [L,R=301,NC]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

htaccess subdirectory + 301 on www

I'd like to do 2 things with htaccess but I can't figure out how to do it.
Let's say my domain is domain.com
First, I'd like to force www. in the url with a 301 redirect.
Another thing, my website is not hosted in the root directory but in /laravel/public/
So I'd like to set this subdirectory as root, and remove it from the URL if someone try www.domain.com/laravel/public/ => www.domain.com
How can I do that?
Thanks in advance.
Assuming that you can't change the document root to point to your public folder, you can try adding these rules to document root (not your public folder):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{THE_REQUEST} \ /+lavarel/public/([^\?\ ]*)
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/laravel/public/
RewriteRule ^(.*)$ /laravel/public/$1 [L]
You'll need to have your own .htaccess file in the root of your domain, along with another in the laravel/public directory (there already is one there, but you need to modify it.
Below are the two files in full as I have them on my testing server.
/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Rewrite requests to the laravel/public index
RewriteCond %{REQUEST_URI} !^/laravel/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /laravel/public/$1 [L]
</IfModule>
/laravel/public/.htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Force www. (also strips laravel/public)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Strip laravel/public, prevent loops
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_URI} ^/laravel/public(/.+)/? [NC]
RewriteRule ^ %1 [R=301,L]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>