I want to remove index.html from url - .htaccess

I have setting up htaccess of my laravel website. I have redirected almost urls. But When I trying to remove index.html from url, it is not working. What can I do for remove index.html from url?
I want to remove index.html from url https://www.example.com/index.html. Right now it gives 404 page not found
I have tried simple redirect but it gives to many redirects,Redirect 301 /index.html https://www.example.com/
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
Second try
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Third try
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://example.com/ [R=301,L]
RewriteCond %{THE_REQUEST} \.html
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

Related

redirecting to localhost dashboard instead of project root

I have a site in localhost - ourallnews. I want to redirect all keyword - index, index.php, index.html to site root - localhost/ourallnews/. I have applied following rule in .htaccess but it is redirected to - localhost/dashboard/. How to fix it ?
RewriteEngine on
RewriteBase /ourallnews/
RewriteRule ^(.*)index\.(php|html?)$ /$1 [R=301,NC,L]
RewriteCond %{THE_REQUEST} /category(?:\.php)?\?cat=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)$ category.php?cat=$1 [L,QSA]
If you're using a RewriteBase then just use a relative link while redirecting:
RewriteEngine on
RewriteBase /ourallnews/
RewriteRule ^(.*)index\.(?:php|html?)$ $1 [R=301,NC,NE,L]
RewriteCond %{THE_REQUEST} /category(?:\.php)?\?cat=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ category.php?cat=$1 [L,QSA]
Make sure to clear browser cache or use a new browser for your testing.

How to redirect to relative url htaccess apache 2.22

I'm trying to redirect the rootdomain.com/index.php to rootdomain.com, and just the root one, not any subdirectories also ending in index.php
I've tried
Redirect 302 ^(.*)index.php$ /$1/
Redirect 302 ^(.*)index.php$ $1/
RewriteRule ^(.*)index.php$ $1/ [R=302,L]
RewriteRule ^(.*)index.php$ / [R=302,L]
RewriteRule ^(.*)index.php$ $1/ [R=302,L,QSD]
RewriteRule ^(.*)index.php$ / [R=302,L,QSD]
(I realize there are other questions like this. But the solutions in the other questions do not work for me)
You can use this generic rule to remove index.php from any relative or root path:
# remove index.php from root
RewriteCond %{THE_REQUEST} \s/+index\.php[?\s] [NC]
RewriteRule ^ / [L,R=301,NE]
Try this in your .htaccess, it should remove index.php from your URL:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

Redirecting a page to another while reporting 301

I have a page at example.com/news.php. This page is already indexed by Google. I would like to ditch this page and instead use example.com/news.
So essentially I need to report 301 on news.php and also redirect any request to /news to news.php.
When I am using the below in .htaccess I am getting a redirect loop:
Redirect 301 /news.php http://example.com/news
RewriteRule ^news$ news.php [L]
How would I go about doing the desired?
Try :
RewriteEngine on
RewriteCond %{THE_REQUEST} /news\.php [NC]
RewriteRule ^ /news [L,R=301]
RewriteRule ^news/?$ news.php [L]
Edit as per your comment :
To redirect any php file , you can use :
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/?$ $1.php [L]

.htaccess redirect all index.html to parent folder

I need to redirect all index.html like this
Original URL
www.example.com/lp/index.html
www.example.com/sytem/index.html
Desired URL
www.example.com/lp
www.example.com/sytem
I used the follwing, it redirects successfully but 404 page
RewriteEngine On
RewriteRule ^index\.html$ [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
You almost got it right, what you did looks like a copy paste error to me. Your first RewriteRule is missing a parameter - there should be a slash before [R=301,L]
Your htaccess should look like this:
RewriteEngine On
RewriteRule ^index\.html$ / [R=301,L]
RewriteRule ^(.*)/index\.html$ /$1/ [R=301,L]
Reference:
http://dense13.com/blog/2012/10/29/removing-index-html-with-mod_rewrite-in-htaccess/
Place this code in your DOCUMENT_ROOT/.htaccess file:
DirectoryIndex index.html
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.html [NC]
RewriteRule ^(.*?)index\.html$ /$1 [L,R=301,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1/index\.html -f [NC]
RewriteRule ^(.+?)/?$ /$1/index.html [L]
Try putting this in your .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1/index\.html [PT,L]
PT and L are rewrite flags, for more details about them check http://httpd.apache.org/docs/2.2/rewrite/flags.html
why not redirect index.(anything) ? like index.php? index.xhtml?
RewriteEngine On
RewriteRule ^index\.(.*)$ / [R=301,L]
RewriteRule ^(.*)/index\.(.*)$ /$1/ [R=301,L]
or to base it from the initial URL request only (to avoid conflicts with other rewrite rules):
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.(.*) [NC]
RewriteRule ^(.*?)index\.(.*)$ /$1 [R=301,L]

htaccess redirect from domain.com/index.php?page=about to domain.com/about

I've upgraded an old website and wanted to redirect all the old pages to the new ones. the old website used the index page and a query string to server the relevant content.
here is the new htaccess file. the problem in question is getting the redirect to work. also if you can steamline this file:
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain.co.uk$ [NC]
RewriteRule ^(.*)$ http://domain.co.uk/$1 [R=301,L]
#if its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
#and it has a trailing slash then redirect to URL without slash
RewriteRule ^(.+)/$ /$1 [L,R=301]
#this removes php extention
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# stops you accessing url with.php
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.?\ ]+)\.php
RewriteRule ^([^.]+)\.php(/.+)?$ /$1%{PATH_INFO} [R=301]
RewriteRule ^news/([^/]*)/([^/]*)$ /news?id=$1&title=$2 [L]
Redirect 301 index?page=about http://domain.co.uk/about
ErrorDocument 404 http://domain.co.uk/404
The apropiate rule to redirect domain.com/index.php?page=about to domain.com/about is the following
RewriteRule ^index\.php$ %{QUERY_STRING} [C]
RewriteRule page=(.*) /$1? [R=301,L]

Resources