Home > Enterprise >  using routes with php
using routes with php

Time:04-01

I have this .htaccess file and I have no knowledge with mod_rewrite,

RewriteEngine On
RewriteRule ^(. [^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/([^.] )\.php [NC]
RewriteRule ^ /%1 [NE,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /$1.php  [L]

What I want to achieve is to have localhost/viewticket/${id} instead of localhost/viewticket.php?id=123

I have tried many htaccess but none worked except this one that hides .php in my URL

CodePudding user response:

Make sure you've changed your internal URLs so you are linking to /viewticket/<id>. Then add the following at the top of the .htaccess file:

# Prevent MutliViews rewriting "viewticket" and omitting the URL parameter
Options -MultiViews

# Redirect any direct requests to "/viewticket.php?id=<id>" to "/viewticket/<id>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^id=(\d*)
RewriteRule ^(viewticket)\.php$ /$1/%1 [R=301,L]

# Rewrite "/viewticket/<id>" to "viewticket.php?id=<id>"
RewriteRule ^(viewticket)/(\d*)$ $1.php?id=$2 [L]

CodePudding user response:

You can use the following code:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/] )/$ $1.php
RewriteRule ^([^/] )/([^/] )/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L

And if it won't works you can read this PDF file: How to remove PHP or HTML extensions with htaccess

  • Related