2

I'm trying to automatically rewrite a URL to a virtual directory using .htaccess.

My rules are working but not automatically, so for example I have a file called login.php in the root directory and I want to rewrite to to a virtual directory to be like this example.com/login/ instead of example.com/login.php, the below rule works fine for this purpose.

RewriteOptions inherit
RewriteEngine On
RewriteBase /

RewriteRule ^login/$ ./login.php [L,NC]

However, this won't rewrite the login.php file automatically. I need whenever a user requests the login.php file, the rule automatically redirects the user to example.com/login/ and still serve the same content. How could this be achieved?

1 Answer 1

1

You will need to a redirect for that:

ewriteOptions inherit
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /login\.php[\s?] [NC]
RewriteRule ^ /login/ [R=301,L]

RewriteRule login/$ ./login.php [L,NC]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.