Home > Back-end >  htaccess file make redirection to another file
htaccess file make redirection to another file

Time:11-24

I have htaccess file below

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

Redirect /index.html /login.html

ErrorDocument 404 /404.html
ErrorDocument 403 /403.html


Options -Indexes

Redirect /index.html /login.html

When user enters index page it should redirect to login.html page. But it doesn't work.

CodePudding user response:

Redirect /index.html /login.html

im requesting /index

If you are requesting /index then I'm not sure why you are checking for /index.html in your rule. However, you should be using mod_rewrite to construct this redirect since you are already using mod_rewrite for your internal rewrites.

So, have it like this instead:

Options -Indexes

ErrorDocument 404 /404.html
ErrorDocument 403 /403.html

RewriteEngine On
RewriteBase /

# Redirect "/index" to "/login"
RewriteRule ^index$ /login [R=302,L]

# Append ".html" if target file exists
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule (.*) $1.html [L]

I've also "fixed" your .html rewrite since this could result in a 500 error for certain requests and there's no need to check that the request does not map to a directory before checking that the request does map to a file.

See the following question on ServerFault that expands on this potential issue regarding .html removal. https://serverfault.com/questions/989333/using-apache-rewrite-rules-in-htaccess-to-remove-html-causing-a-500-error

  • Related