Mina Hafzalla
Mina Hafzalla

Reputation: 2821

Automatically rewriting URLs using .htaccess?

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?

Upvotes: 2

Views: 246

Answers (1)

anubhava
anubhava

Reputation: 785521

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]

Upvotes: 1

Related Questions