Question

I want to simply have the URL:

example.com/connect/admin/create

Redirect to:

example.com/connect/admin/create.php

I have the code below on the .htaccess file:

RewriteEngine On
RewriteBase /
RewriteRule ^connect/admin/create$ http://example.com/connect/admin/create.php [R=301,L,NE]

And it redirects properly. The issue is that the browser now displays

example.com/connect/admin/create.php

When I still want it to display:

example.com/connect/admin/create

Do I need to add something else to .htaccess?

Thank you

Était-ce utile?

La solution

Your original rewrite rule contains an explicit redirect via R=301. That flag will need to be removed. Secondly, the right-side of the RewriteRule (the target) include http://example.com/ which will also force a browser redirection implicitly. Instead, use just the relative path on the right side:

RewriteEngine On
RewriteBase /
RewriteRule ^connect/admin/create$ connect/admin/create.php [L,NE]

This can be done in a more generic way if you will also have additional actions inside connect/admin from which you'd like to strip the .php:

# Capture the action into $1 and pass it to the redirect target.
RewriteRule ^connect/admin/([^/.]+)/?$ connect/admin/$1.php [L,NE]
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top