[Q] How to redirect to specific file?

Your example will already do that if there is no index.html or index.htm or index.shtml.
 
index.php is already one of the default pages that apache looks for if a file is not specified in the url.
 
No its not redirected. Its a default page. index.php shows up without a redirect.

Going to http://www.example.com will show these pages automatically and in this order:

index.html
index.htm
index.shtml
index.php

Its not a redirect.
 
No its not redirected. Its a default page. index.php shows up without a redirect.

Going to http://www.example.com will show these pages automatically and in this order:

index.html
index.htm
index.shtml
index.php

Its not a redirect.
Ok.
So how can i redirect it to "/index.php" instead of "/"?
 
There are two types of redirect: an alias/pointer, or a real redirect.

In the first case, Apache get a request and answers with the correct data as if it were the requested data.
For example, the directive DirectoryIndex in httpd.conf is a list of files Apache will look into (one by one, until one exists) and give as answer to the "/" request. That's why when you open www.domain.example/ you will receive www.domain.example/index.php
If that's what you want, check DirectoryIndex.

The second case triggers an HTTP 3xx Redirect code, it's a special answer that tells the client to look for another file. For example RewriteRule will do it by default.
If you want to see "/index.php" in the client when looking for "/", you will have to insert this in a ".htaccess" file in the same directory:
Code:
RewriteEngine On
RewriteRule ^/$ /index.php [R=301,L]
The 301 code is a permanent redirect, this way most search engines will know "/" doesn't exist anymore and add it's ranking value to "/index.php".
 
There are two types of redirect: an alias/pointer, or a real redirect.

In the first case, Apache get a request and answers with the correct data as if it were the requested data.
For example, the directive DirectoryIndex in httpd.conf is a list of files Apache will look into (one by one, until one exists) and give as answer to the "/" request. That's why when you open www.domain.example/ you will receive www.domain.example/index.php
If that's what you want, check DirectoryIndex.

The second case triggers an HTTP 3xx Redirect code, it's a special answer that tells the client to look for another file. For example RewriteRule will do it by default.
If you want to see "/index.php" in the client when looking for "/", you will have to insert this in a ".htaccess" file in the same directory:
Code:
RewriteEngine On
RewriteRule ^/$ /index.php [R=301,L]
The 301 code is a permanent redirect, this way most search engines will know "/" doesn't exist anymore and add it's ranking value to "/index.php".

That don't work :(

That my .htaccess file(look just at the bold text, other rules it's just for plugins in my vBulletin forum)

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.
example\.com/$1 [R=301,L]
RewriteRule ^/$ /index.php [R=301,L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^videos\.html&page=([0-9]+)$ http://www.example\.com/videos-page$1.html [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^videos\.html$ index.php?videos=1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^videos-page([0-9]+)\.html$ index.php?videos=$1 [L]

RewriteRule ^((urllist|sitemap).*\.(xml|txt)(\.gz)?)$ vbseo_sitemap/vbseo_getsitemap.php?sitemap=$1 [L]

Thanks!
 
Last edited:
I haven't time for testing it myself right now, but I suggest you try this:
Code:
$ nc -v www.yoursite.example 80
GET / HTTP/1.1<Enter>
Host: www.yoursite.example<Enter>
<Enter>
You should se this as answer:
Code:
HTTP/1.1 301 Found
Date: [...]
Server: Apache[...]
Location: /index.php
[...]

If you receive a 200 code instead, try removing the previous lines. The "L" should work as "end of rules block", but I can't see any other explanation.
 
I haven't time for testing it myself right now, but I suggest you try this:
Code:
$ nc -v www.yoursite.example 80
GET / HTTP/1.1<Enter>
Host: www.yoursite.example<Enter>
<Enter>
You should se this as answer:
Code:
HTTP/1.1 301 Found
Date: [...]
Server: Apache[...]
Location: /index.php
[...]

If you receive a 200 code instead, try removing the previous lines. The "L" should work as "end of rules block", but I can't see any other explanation.
I get Error 500(Internal Server Error), when i put this code into .htaccess file(instead of my older content).
 
Back
Top