Reputation: 17553
I am trying to use mod_rewrite to generate cleaner urls.
I have the following in my .htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^mypage.php$ https://%{HTTP_HOST}/mypage [R=301,L]
The objective is to go from https://mysite.com/mypage.php to https://mysite.com/mypage
This gives me a 404 error. I don't actually have the directory mypage/ existing. But from my understanding, I don't need to actually have mypage for mod_rewrite to work. What am I doing wrong?
Upvotes: 0
Views: 110
Reputation: 143886
does mod_rewrite output have to exist?
The substituted target of a rewrite rule either must exist or match some other rule whose substituted target exists. What you've got simply redirects the browser to a URL that doesn't exist, so you will without a doubt get a 404 error.
You could add another rule to rewrite /mypage
back to /mypage.php
, but then you'd create an infinite loop. "mypage.php" would get redirected to "mypage", which would get rewritten to "mypage.php", which would get redirected to "mypage", etc.
You need to add an additional constraint to your rule above:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /mypage\.php
RewriteRule ^mypage.php$ https://%{HTTP_HOST}/mypage [R=301,L]
will make it so it only redirects if the actual request was mde for /mypage.php
, and not just check against the URI, which can get changed by other rules. Then you'd just need to internally rewrite it back:
RewriteRule ^mypage/?$ /mypage.php [L]
See the top of this answer for a better explanation of how this works: https://stackoverflow.com/a/11711948/851273
Upvotes: 1