trouble matching pattern against querystring for mod_rewrite in .htaccess file - php

I have 4 urls in my project that I am trying to translate using mod_rewrite
hall/description_banquethall.php?hall_id=1 > hall/description_banquethall/1
hall/venues.php?city=hyderabad&city_value=1&lacality=malakpet&locality_value=82 > hall/venues/hyderabad/1/malakpet/82
hall/hall_booking.php?hall_id=1&booking_date=2014-10-23&session=Morning > hall/hall_booking/1/2014-10-23/Morning
hall/booking_message.php?booking_id=10 > hall/booking_message/10
So far, I have the following in .htaccess which works for
hall/booking.php?hall_id=5&booking_date=2014-10-23&session=Morning
and
hall/description_banquethall.php?hall_id=5
but not for the two URLs listed above:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /hall/
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?city=([^&\s]+)&city_value=([^&\s]+)&locality=([^&\s]+)&locality_value=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ $1.php?city=$2&city_value=$3&locality=$4&locality_value=$5 [L,QSA]
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+)&booking_date=([^&\s]+)&session=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3/%4? [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/([^/]+)/([^/]+)/([^/]+)/?$ $1.php?hall_id=$2&booking_date=$3&session=$4 [L,QSA]
# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+) [NC]
RewriteRule ^ %1/%2/? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?hall_id=$2 [L,QSA]
# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?booking_id=([^&\s]+) [NC]
RewriteRule ^ %1/%2/? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?booking_id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?booking_id=$2 [L,QSA]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d

Why don't u use this in your .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule
^(.*)/?$ index.php?page=$1 [L]
</IfModule>
Then in your index.php you return the appropriate page:
# SELECT WHICH PAGE TO DISPLAY
function getPage($showPage) {
$modules = array(#never name the slugs as same as the php pages
'/sign-in/i' => 'register.php',
'/signout/i' => 'logout.php',
'/staff/i' => 'admin.php',
'/products/i' => 'blank.php',
);
foreach ($modules as $slug=>$phpmodule) {
if(preg_match($slug, $showPage)) {
return $phpmodule;
}
}
return 'blank.php';#in case module not found display 404 page
}
if (isset($_GET['page'])) {
require_once (getPage($_GET['page'])); #include the appropriate module file
}

Related

Blank Space / Whitespace Character for .htaccess

this rule is working for me but i have a problem
www.domain.com/description_functionhall/117/A%2520S%2520Garden
second rule is taking %2520 so i want that url in this format
www.domain.com/description_functionhall/117/A-S-Garden
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /function_hall_project/
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+)&booking_date=([^&\s]+)&session=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3/%4? [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/([^/]+)/([^/]+)/([^/]+)/?$ $1.php?hall_id=$2&booking_date=$3&session=$4 [L,QSA]
# To externally redirect /dir/foo.php?hall_id=123&name=abcd to /dir/foo/123/abcd
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+)&name=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3? [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/([^/]+)/([^/]+)/?$ $1.php?hall_id=$2&name=$3 [L,QSA]
# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+) [NC]
RewriteRule ^ %1/%2/? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?hall_id=$2 [L,QSA]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]
You can insert this rule just below ## hide .php extension snippet line to convert all these white spaces to hyhen:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%2520|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R=302]

Original url: hall/booking.php?hall_id=1&booking_date=2014-10-23&session=Morning I want: hall/booking/1/2014-10-23/Morning

i have one url in my project like
hall/description_banquethall.php?hall_id=1 and my url is working like hall/description_banquethall/1
by using this .htaccess code
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /hall
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+) [NC]
RewriteRule ^ %1/%2/? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?hall_id=$2 [L,QSA]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]
now i want to redirect my another url hall/booking.php?hall_id=1&booking_date=2014-10-23&session=Morning to hall/booking/1/2014-10-23/Morning
Below ## hide .php extension snippet line add these 2 new rules:
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+)&booking_date=([^&\s]+)&session=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3/%4? [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/([^/]+)/([^/]+)/([^/]+)/?$ $1.php?hall_id=$2&booking_date=$3&session=$4 [L,QSA]

Sub-domain Redirect Using htaccess

I am working on a project that requires rewriting a URL like
https://orange.url.com
to
https://www.url.com/?s=orange
I need assistance on how to churn out htaccess to handle this task.
PS: Please not that I already have the following in my htaccess handling some rewriting for me.
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
You can put this code in your htaccess (which has to be in your root folder)
Options -MultiViews
RewriteEngine On
RewriteBase /
# To internally forward http://xxx.domain.com to http://www.domain.com/?s=xxx
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.example\.com$ [NC]
RewriteRule ^$ index.php?s=%1 [L]
# To internally forward http://xxx.domain.com/yyy/ to http://www.domain.com/?s=xxx&c=yyy
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.example\.com$ [NC]
RewriteRule ^([^/]+)/$ index.php?s=%1&c=$1 [L]
# To internally forward http://xxx.domain.com/yyy/zzz/ to http://www.domain.com/?s=xxx&c=yyy&g=zzz
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.example\.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/$ index.php?s=%1&c=$1&g=$2 [L]
# To externally redirect /dir/foo.php to /dir/foo/
RewriteCond %{THE_REQUEST} \s/(.+?)\.php [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1/ [R,L]
# To internally forward /dir/foo/ to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/$ $1.php [L]

formatting URL parameters using mod_rewrite

First let me show you my .htaccess file code below
RewriteEngine On
RewriteBase /site/public/admin/
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
The code above works fine but what I would like to add to this is when I type the following URL
http://localhost/site/public/admin/AccidentDetails/?caseid=12
it should change to the following
http://localhost/site/public/admin/AccidentDetails/caseid/12/
Not good at mode rewriting I would like have your opinion on that, please help.
You can use this .htaccess:
RewriteEngine On
RewriteBase /site/public/admin/
RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+) [NC]
RewriteRule ^ %1/caseid/%2/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/caseid/([^/]+)/?$ $1.php?caseid=$2 [L,NC,QSA]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=302,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Right below your RewriteBase line, add:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+?)(?:\.php|)\?caseid=([0-9]+)
RewriteRule ^ %1/caseid/%2/? [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/caseid/([0-9]+)/$ $1?caseid=$2 [L]
add the following code below the RewriteBase line
RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+) [NC]
RewriteRule ^ %1/caseid/%2/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/caseid/([^/]+)/?$ $1.php?caseid=$2 [L,NC,QSA]

Mode Rewrite added another url parameter but not working (multiple parameters)

I have the following htaccess file code below
RewriteEngine On
RewriteBase /site/public/admin/
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+) [NC]
RewriteRule ^ %1/caseid/%2/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/caseid/([^/]+)/?$ $1.php?caseid=$2 [L,NC,QSA]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=302,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
This works perfectly fine as long as I provide 1 parameter which is caseid i.e
http://localhost/site/public/admin/PIClaimant.php?caseid=11
The URL above successfully changes to
http://localhost/site/public/admin/PIClaimant/caseid/11/
I've had a few pages with two parameters, which are PIClaimant.php?caseid=11&picid=34. Initially when I was creating htaccess file I did not pay attention to those pages (&picid=34 only being used at a few pages 4 or 5 max). Now when I go back to those pages which has those parameters, they are not working not showing the content of page. They work till first parameter PIClaimant/caseid/11/ but not with the second parameter.
Since I am not very good at this (mode rewriting) I need your help.
Any Ideas?
Things are getting more complicated now :)
Try this code in your .htaccess:
RewriteEngine On
RewriteBase /site/public/admin/
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+)?\&picid=([^&\s]+)\s [NC]
RewriteRule ^ %1/caseid/%2/picid/%3/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/caseid/([^/]+)/picid/([^/]+)/?$ $1.php?caseid=$2&picid=$3 [L,NC,QSA]
RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+)\s [NC]
RewriteRule ^ %1/caseid/%2/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/caseid/([^/]+)/?$ $1.php?caseid=$2 [L,NC,QSA]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=302,L,NE]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Change your code to the code below:
RewriteEngine On
RewriteBase /site/public/admin/
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
#RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+) [NC]
RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+)?\&picid=([^&\s]+) [NC]
RewriteRule ^ %1/caseid/%2/picid/%3/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([^/]+)/caseid/([^/]+)/?$ $1.php?caseid=$2
RewriteRule ^([^/]+)/caseid/([^/]+)/?$ $1.php?caseid=$2&picid=$3 [L,NC,QSA]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=302,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
replace the code with your code see if that works

Categories