Apache not showing index.html on address bar - apache

I would like to show my index.html file on address bar. When you go to http://www.example.com/ to redirect it to http://www.example.com/index.html and show that index.html on address bar.
EDIT
This is my .htaccess file. I have try neo example but with no luck.
DirectoryIndex naslovnica.php
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* 404.html [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /naslovnica\.php
RewriteRule ^naslovnica\.php$ http://example.com/naslovnica.php [R=301,L]

You will have to look into Apache DocumentRoot Directory. Have a look at the following Apache Docs...
Apache DocumentRoot
You will have to create the index.html and put it in the DocumentRoot directory.
From the DOCS...
If your DocumentRoot "/usr/web" then an access to http://my.example.com/index.html refers to /usr/web/index.html.
Edited
The following document explains about hiding the index. Guess you can use it to unhhide the index as well.
Mod_Rewrite – Hide index.php
The following might be a start towards finding the right solution...
Options +FollowSymLinks
RewriteEngine on
# Redirect client requests for www.example.com/ to "www.example.com/index.html" in main doamin
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html
RewriteRule ^index\.html$ http://www.example.com/index.html [R=301,L]

Related

htaccess redirect directory to single page in same directory

All attempts failed... I want http://olex-design.at/journal to to be redirected to http://olex-design.at/journal/alle through .htaccess.
So far I have the following - non working - rule in place which produces a 403 forbidden page.
RewriteRule ^journal/$ /journal/alle [R=301,L]
The .htaccess file is placed in the root directory. Maybe someone can point me towards the right direction...
All rules in .htaccess files:
Options -Indexes -MultiViews
RewriteEngine on
#Force non-www:
RewriteCond %{HTTP_HOST} ^www\.olex-design\.at [NC]
RewriteRule ^(.*)$ http://olex-design.at/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
RewriteRule ^journal/$ /journal/alle [R=301,L]
ErrorDocument 404 /404
With the support of my webhost I was able to resolve my issues described above. This is the complete code in my .htaccess file.
Please note that the first 4 lines did the trick. The code below is responsible to remove the "www", remove the "index" when your in home (http://olex-design.at) and remove the .html file extension.
RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^journal\/?$ "http\:\/\/olex\-design\.at\/journal\/alle" [R=301,L]
AddDefaultCharset utf-8
Options -Indexes -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.olex-design\.at [NC]
RewriteRule ^(.*)$ http://olex-design.at/$1 [L,R=301]
<IfModule mod_rewrite.c>
#index redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\ HTTP/
RewriteRule ^index\.html$ http://olex-design.at/ [R=301,L]
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

In .htaccess to prioritize DirectoryIndex to a RewriteRule

I want files to be accessed when they are available as specified in the DirectoryIndex line. E.g. when someone types in www.mywebsite.com/mydir/ it will redirect correctly. For this I have in my .htaccess the line:
DirectoryIndex index.html default.php
When a filename or directory does not exist I want it to redirect to a file (target_handler.php) which is located in the same directory as my .htaccess file. For this I have placed several lines after the DirectoryIndex:
RewriteEngine On
RewriteCond %{ENV:URI} ^$
RewriteRule ^(.*)$ - [ENV=URI:$1]
RewriteCond %{ENV:BASE} ^$
RewriteCond %{ENV:URI}::%{REQUEST_URI} ^(.*)::(.*?)\1$
RewriteRule ^ - [ENV=BASE:%2]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ %{ENV:BASE}target_handler.php [L,QSA]
What I want is for Apache to first check to see if there is an index.html or default.php available, and only upon unavailability perform a rewrite. Currently the behaviour seems random(?), so if someone types www.mywebsite.com/mydir/ then Apache sometimes does the rewrite first without checking if there is an index.html in the specified folder.
Help appreciated
PS: I am testing using: xampp
Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.15
You can omit DirectoryIndex and do it all using mod_rewrite itself:
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}index\.html -f
RewriteRule ^ index.html [L]
RewriteCond %{DOCUMENT_ROOT}%{ENV:BASE}default\.php -f
RewriteRule ^ default.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^target_handler\.php$ target_handler.php [L,NC]

Consolidating .htaccess and fix bug

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.

Symfony2 - Rewrite url to add www prefixwith existing .htacess file

Symfony2 core configuration comes up with the following .htacess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
I understand that this will erase the app.php from the url in the browser.
I would like to add the following rewrite rule to add www. to my WebSite everytime a user enter the domain name without the www. :
RewriteCond %{HTTP_HOST} ^valutao\.com$ [NC]
RewriteRule ^(.*)$ http://www.valutao.com/$1 [L,R=301]
So now i have the following .htacess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
RewriteCond %{HTTP_HOST} ^valutao\.com$ [NC]
RewriteRule ^(.*)$ http://www.valutao.com/$1 [L,R=301]
</IfModule>
My issue is that app.php will show up again. It seems that my rule just hides the base configuration. Here is an example of what is displayed in the url when I enter valutao.com : http://www.valutao.com/app.php
Does someone know how to fix that. Unfortunately I have just basic knowledge with the rewrite rules.
Thanks for help
You simply need to change the order (www rewrite should go first).
I'd also make it less dependent on the domain name:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>

Under what situation will .htaccess not work for Apache?

vi .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|audio|editor|upload|images|js|css|robots\.txt|sitemap\.xml)
RewriteRule ^(.*)$ /index.php?$1 [L]
When I try to browse http://wolaitiao.cn/install,it reports 404,so it's not working correctly.
Is it because it's a VirtualHost?
<VirtualHost *:80>
...
Check apache error log
first make sure Rewrite is enabled for Apache and .htaccess files are honored for the rules.
then you can simply redirect anything that does not physically exist to index:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [QSA,L]