skip to Main Content

I am trying to do a simple rewrite rule using an htaccess file. I have this page hosted on localhost using XAMPP:

localhost/naveesh/services.php

A possible way to request this is like this:

localhost/naveesh/services?serv=Boat%20Dealers

What I am trying to do is rewrite the requests to the following URL to the above URL

localhost/naveesh/services/Boat-Dealers

I tried this but it gives me a 404 error.

RewriteEngine On
RewriteRule ^/naveesh/services/([^/]+)/?$ /naveesh/services.php?serv=$1 [L,QSA]

After some research I have tried another option:

RewriteEngine On
RewriteBase /naveesh/

# Hide .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

RewriteRule ^services/([^/]+)/?$ /services.php?serv=$1 [L,QSA]

But, it is giving too many internal redirects issue now

2

Answers


  1. RewriteRule you are using looks fine as of my knowledge, but slash in the pattern may not match the path when the .htaccess file is located in a subdirectory. use this

    RewriteEngine On
    RewriteBase /naveesh/
    RewriteRule ^services/([^/]+)/?$ services.php?serv=$1 [L,QSA]
    

    should match the URL correctly and rewrite the request to the correct URL. If you are still getting a 404, make sure that mod_rewrite is enabled in your Apache configuration and that AllowOverride is set to All for the directory where the .htaccess file is located.

    Login or Signup to reply.
  2. RewriteEngine On
    RewriteBase /naveesh/
    
    # Hide .php extension
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*?)/?$ $1.php [L]
    
    RewriteRule ^services/([^/]+)/?$ /services.php?serv=$1 [L,QSA]
    

    It is your first rule ("Hide .php extension") that causes the "too many internal redirects" since it (incorrectly) rewrites the request to services/Boat-Dealers.php to services/Boat-Dealers.php.php etc. This is because the condition (that is always successful) is not checking the same thing that you are ultimately rewritting to. REQUEST_FILENAME is not what you expect it to be. (Although reversing the order of these rules could resolve this immediate issue.)

    See my answer to the following question on ServerFault for more detail on this specific issue.

    However, the second rule is also incorrect (as addressed in @Faizal’s answer).

    These rules are also in the wrong order.

    Assuming your .htaccess file is located at /naveesh/.htaccess (not the document root) then your complete .htaccess file should look like this instead:

    # Disable MultiViews
    Options -MultiViews
    
    RewriteEngine On
    
    RewriteRule ^services/([^/]+)/?$ services.php?serv=$1 [QSA,L]
    
    # Hide .php extension
    RewriteCond %{DOCUMENT_ROOT}/naveesh/$1.php -f
    RewriteRule ^(.*?)/?$ $1.php [L]
    

    Note the slash prefix on the substitution string is removed.

    The RewriteBase directive is not required here. Relative substitutions are relative to the directory where the .htaccess file is located.

    You need to ensure that MultiViews is disabled, since this rule will not work as expected if it is. The request would be rewritten to services.php but without the serv URL parameter. (MultiViews is sometimes enabled on some shared servers and is the cause of many conflicts.)

    Additionally, you should not really allow an optional trailing slash on the requested URL. This potentially opens you up to a duplicate content issue, since you have two different URLs (one with and one without the trailing slash) that serve the same content. There is no trailing slash on your example URL, so why include it? If you need to allow for an optional trailing slash (eg. if you have some erroneous inbound links to the non-canonical URL) then you should externally redirect one to the other instead.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search