2

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 ^(.*?)/?$ $1.php [L]

# To remove www header
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$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?

  • I'm not sure why you would need the extension (and to make an exception) on the search URLs? Why does the search page not work when the other pages work OK - why is the search page any different? The point of doing this is presumably to remove the file extension from all URLs. Does your page link to search or search.php (it should be the former). – MrWhite Mar 9 '15 at 17:27
  • The search page has an extension of .php but is taken out because of the htaccess rules.ogsantosconstruction.com – telo78 Mar 9 '15 at 17:54
  • In order to implement this correctly, all your internal links should be without the .php extension as well, otherwise all your navigation is going to result in an external redirect (2 requests) - which is what's happening. – MrWhite Mar 9 '15 at 18:59
  • ...Although this isn't directly related to your problem with the search. – MrWhite Mar 9 '15 at 19:33
  • I'm not sure why you'd want to do this. Search engines don't care which software powers your website, and most users wouldn't either. What's the goal? – John Mueller Mar 11 '15 at 10:43
1
# 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$ $1 [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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.