Home  >  Q&A  >  body text

Use htaccess to rewrite a directory's URL into a PHP file

I'm trying to perform simple rewrite rules using htaccess files. I'm hosting this page on localhost using XAMPP:

localhost/naveesh/services.php

Possible ways to request this operation are as follows:

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

What I want to do is rewrite the request for the following URL to the above URL

localhost/naveesh/services/Boat-Dealers

I tried this but it gave me a 404 error.

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

After some research, I tried another option:

RewriteEngine On
RewriteBase /naveesh/

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

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

However, there are now too many internal redirection issues

P粉957723124P粉957723124306 days ago411

reply all(1)I'll reply

  • P粉343408929

    P粉3434089292024-01-11 16:05:46

    It's your first rule ("Hide .php extension") that causes "Too many internal redirects" because it (incorrectly) targets services/Boat-Dealers.php The request is rewritten to < code>services/Boat-Dealers.php.php etc. This is because the condition (which always succeeds) doesn't check what you end up overriding. REQUEST_FILENAME is not what you expected. (Although reversing the order of these rules would solve this immediate problem.)

    Please seeMy comment on the followingIssue on ServerFault< /a> More details on this specific issue.

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

    The rules are also in the wrong order.

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

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

    Note that the slash prefix on the replacement string has been removed.

    No RewriteBase directive is required here. Relative replacement is relative to the directory where the .htaccess file is located.

    You need to make sure that MultiViews is disabled as this rule will not work as expected if disabled. The request will be rewritten as services.php without the serv URL parameter. (Multiple views are sometimes enabled on some shared servers, which can cause many conflicts.)

    Additionally, you shouldn't really allow optional trailing slashes on requested URLs. This can lead to duplicate content issues because you have two different URLs (one with a trailing slash and one without a trailing slash) serving the same content. There is no trailing slash in your example URL, so why include it? If you need to allow optional trailing slashes (for example, if you have some bad inbound links pointing to non-canonical URLs), then you should externally redirect one link to another.

    reply
    0
  • Cancelreply