Ronny-André Bendiksen Ronny-André Bendiksen - 1 month ago 14
PHP Question

How to use RewriteRule like WordPress and Drupal

I have a website with an include script that will have a various number of variables.

Here are some of the types of URLs and variables:

Main page: index.php

Login page: login.php (redirects to index.php after login).

Logout page: logout.php (redirects to login after logout).

Profile page: index.php?id=profile

Users password page: index.php?id=profile&sub=password


And some pages even has more variables in the URL. I want to use RewriteRules to be able to get nice URLs like this:

url.com/my/site/ (base)

url.com/my/site/profile/ (profile page)

url.com/my/site/profile/password/ (password page)

url.com/my/site/profile/password/variable1/variable2/variable3/ (use variables in the URL)


How can I do this in the .htaccess-file? The site is not the root on the domain, but a couple of folders down.

Answer

.htaccess rules apply to its container directory and all of its subdirs. This might be a good starting point:

<IfModule mod_rewrite.c>
    #Removing .php file extension.
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
</IfModule>

Or you might want to remove the index.php's GET parameters from within url:

<IfModule mod_rewrite.c>
    #removing GET parameters.  
    RewriteRule ^index/([^/\.]+)/?$ index.php?id=$1 [L]
</IfModule>

Read more: