We have a rewrite rule setup to map
/areas/spaces/test/ as follows:
RewriteRule ^areas/spaces/?$ spaces/$1.php
This is so the URL is mapped differently, but the underlying logic / code is still
So if we now access
spaces/test.php they both work.
But.. I want a 301 to redirect
Is this possible?
You can use the following rewrite rules to accomplish both things you desire.
RewriteRule ^spaces/([^/.]+).php$ /areas/spaces/$1/ [QSA,R=301,L] RewriteRule ^areas/spaces/?$ spaces/$1.php [L]
I’ll walk you through the first line that I added:
- A request for
- Is rewritten to
- But instead of an internal rewrite, use a redirect
QSAtells Apache to append any query string that was passed onto the new location as well.
Ltells Apache to stop rewriting on that line when it matches, this way a redirect occurs and rewriting stops. On the next request for the proper location, the second line will match as you desire, so that internally it’s the
.phpfile handling the request.
See also: official