: How to remove php extension from pages except search results? Using htaccess I have these php rules: <IfModule mod_rewrite.c> Options +FollowSymLinks -MultiViews # Turn mod_rewrite on RewriteEngine
Using htaccess I have these php rules:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s([^.]+).php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ .php [L]
# To remove www header
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^(.*)$ %1/ [L,R=301]
</IfModule>
Everything is working, except if I do a search, the search results page is not showing the results because it is taking out the php extension. Example: search/?s=building where "building" is the term used. Generally, it should look like this: search.php?s=building
How do I tell htaccess that I only wanted to hide the php extensions of the pages except the results page?
More posts by @Pierce454
1 Comments
Sorted by latest first Latest Oldest Best
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s([^.]+).php [NC]
RewriteRule ^ %1 [R,L]
To exclude your "search" page from the redirect you could include an additional condition. Only redirect when the requested URL is not "search.php".
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s[^.]+.php
RewriteCond %{REQUEST_URI} !search.php
RewriteRule ^(.*).php$ [R=301,L]
I've tweaked the rules a bit to make them more efficient by specifically checking for the ".php" file extension in the RewriteRule. Removed the NC flag as this seems redundant. Changed from R (temporary) to R=301 (permanent) redirect - I presume this should be "permanent"? (Although it's good to test with a temporary - non-cached - redirect.)
However, why the "search" page doesn't work without the file extension would seem to be another issue. It should work! There seems to be another (301) redirect that is redirecting /search?s=world to /search/?s=world?
You also still need to resolve all your internal linking so that you link to URLs without the .php extension otherwise your site is going to be issuing a lot of unnecessary redirects.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.