Waleed Afridi
Waleed Afridi

Reputation: 107

HTACCESS : Replace URL with the help of regex

I have this URL

http://localhost:8888/dochealth/child-doc?cat=doctor

and wanted to replace it with .htaccess REGEX So it becomes

http://localhost:8888/dochealth/child-doc/doctor

so far I have tried this REGEX but it doesn't work

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^child-doc/([0-9a-zA-Z_-]+) child-doc?cat=$1 [NC,L]
</IfModule>

Upvotes: 2

Views: 48

Answers (1)

anubhava
anubhava

Reputation: 786091

Have it this way:

Options -MultiViews
RewriteEngine On

RewriteRule ^child-doc/([\w-]+) child-doc.php?cat=$1 [NC,QSA,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Changes are:

  1. Move .php extension handler rule to the bottom
  2. Use child-doc.php in other rules
  3. Additional of QSA (query string append) in the same rule
  4. Turning off MultiViews to disable content negotiations service of Apache

Upvotes: 2

Related Questions