Remove index.php from url Laravel 5 - php

URLs are working fine in my application. I mean they are pretty URLs. Like http://www.example.com/
But it also works when you access the page with index.php like http://www.example.com/index.php, which I don't want because it is showing two links in sitemap for one page. One page without index.php and another with index.php. Demonstration of the sitemap is here https://www.xml-sitemaps.com/details-eln.6762418.html
Here is the .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Put the following code :
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/(.*)index\.php\sHTTP.*$
RewriteRule ^ /%1 [R=301,L]

Related

How to stop redirection to index.php main file in code igniter?

I have a website domain.com which is based in code igniter. When someone enters a wrong url it redirects it to https://domain.com/index.php
I want to redirect redirect it https://domain.com and do not want index.php to be present in the urls. I have tried everything in htaccess file but it dooesnt work.
Here is my htaccess file
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
AddHandler application/x-httpd-php55 .php55 .php
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
If I understand rightly, you want to redirect index.php to the root?
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
Let me know if not

.htaccess config for search bot crawling

Am build a web app with angularjs and my public root looks like this
-assets
-snapshots
-snapshots/article/
-snapshots/sections/
-snaphosts/index.html
.htaccess
and am using phantomjs to generate snapshots from siteamp, my htaccess look this like this
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# angular seo
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ /snapshots/%1 [NC,L]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
when i try to test the snapshots using the url http://xyx.com/?_escaped_fragment_=/section/news i get http://xyx.com/?_escaped_fragment_=/section/news#!/ instead of http://xyx.com/snapshots/section/news.html as expected.
I figure my .htaccess config could be wrong, am not really good at htaccess configs.
Any help will be appreciated.

Pointing a Sub-domain to a sub directory

Using Laravel I'm generating php pages in to a sub-directory called webpage. I want to allow users to access pages using sub-domain. For example, I've generated a file whoami.php into webpage directory. If people try to access whoami.domain.com, he can see the whoami.php. I've crated an index.php inside webpage which can extract alias and show the file. The think I need is to pass the subdomain to index.php file without redirecting. I wrote htaccess as follow
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
#sub domain custom part
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/webpage/%1/$1 [L,NC,QSA]
</IfModule>
But I'm redirecting into http://domain.com/webpage/whoami.
I tried to make my requirements clearly and pasted what I tried. Can you please help me to figure out a way?
EDIT 1
Based on #Starkeen's answer I tried the following code an now in a redirect loop (http://whoami.domain.com/webpage/whoami/webpage/whoami/......).
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
#sub domain
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule ^(.*)$ /webpage/%1/$1 [L,NC,R]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
To redirect subdomain to a subfolder, You probably need to put the following right after the "RewriteEngine On" line :
RewriteCond %{HTTP_HOST} ^(sub)\.domain\.com$
RewriteRule ^(.*)$ /webpage/%1/$1 [NC,R,L]
I got a every simple solution. As I'm using Laravel and that already use an index.php as bootstrap, I wrote some PHP codes there to achieve my goal. You guys can use if needed. And please advice me if my solution isn't good enough. Thanks
My Solution
$link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$link = str_replace(array('http://', 'https://', 'www.'), '', $link);
$splitted = explode(".", $link);
if($splitted[0] != 'domain'){
$file_name = $splitted[0].'.php';
$file_loc = 'webpage/'.$file_name;
if(file_exists($file_loc)) {
include($file_loc);
} else {
header('Location: http://domain.com/not-found');
}
die();
}

Laravel 5 - URL Rewriting and CSS conflict

I have a problem in Laravel 5 between my .htaccess for the URL rewriting and my CSS files. First of all, here is my project tree:
I have a .htaccess in my public folder as you can see on the picture. Here is the content of my .htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -Indexes
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteRule ^ index.php [L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
</IfModule>
This is the code I use to remove "index.php" in URLs. For example, http://localhost/mySite/public/index.php/about becomes http://localhost/mySite/public/about.
Now, I have a master page (in /resources/views/) and in this master page I would like to reference the css file which is located in /public/css/style.css. I added this code in my master page:
<link rel='stylesheet' href="{{ URL::asset('css/style.css') }}" >
This does not work with the current htaccess but if I comment the line RewriteRule ^ index.php [L] in my htaccess and access to the URL (with "index.php") the stylesheet is well referenced. I think I have to add an other line in htaccess but I have no idea what I have to add.
I hope you can help me and don't hesitate to ask for more details. Thanks.
Why did you change the default .htaccess?
It should be like this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
The two RewriteCond prevent requests for files to be routed to Laravel. But they have to be before the RewriteRule to be effective.

How can I change the URL in the browser by htaccess?

I want to change my url by htaccess from
http://example.com/offer?search=something to http://example.com/offer/something
The following htaccess works from http://example.com/offer/something but if I typing the http://example.com/offer?search=something then the url is not changing to http://example.com/offer/something.
RewriteEngine On
RewriteRule ^offer/([^/]*)$ /offer?search=$1 [L]
Should I use "rewritecond"? What is the correct htaccess in this case?
I try make it in Laravel framwork. I have only one htaccess and this is the content, currently:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{QUERY_STRING} ^search=([-a-zA-Z0-9]+)
RewriteRule ^offer$ /offer/%1? [R=301,L]
RewriteRule ^offer/([^/]*)$ /offer?search=$1 [L]
</IfModule>
UPDATE: Full .htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# redirect URL with GET parameter to pretty URL
RewriteCond %{THE_REQUEST} \s/+offer\?search=([^\s&]+) [NC]
RewriteRule ^ /offer/%1? [R=301,L]
# rewrite from pretty URL to actual handle
RewriteRule ^(offer/[^/]+)/?$ index.php/$1 [L,QSA]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
How it prevents looping: THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite rules, unline %{REQUEST_URI} which gets overwritten after other rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1
You also need to include a redirect from the url that contain a query string. I haven't tested this, but the first rule should catch the first URL and redirect it to the new format. The second rule should catch the new format and pass the correct values to the search parameter.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^search=([-a-zA-Z0-9]+)
RewriteRule ^offer$ /offer/%1? [R=301,L]
RewriteRule ^offer/([^/]*)$ /offer?search=$1 [L]

Categories