JUNAID AFZAL
JUNAID AFZAL

Reputation: 41

URL Rewrite Rule not working .htaccess

I am trying to Rewrite the URL using .htaccess file but it is not working. First I tried a lot on my localhost and then on my Live Server but it's not working.. I still get the original url.That is what I have..

Original URL:-

http://test.1click.com.pk/specific_hall.php?hall_name=Shalimar+Hall&hall_id=1

I Want:-

http://test.1click.com.pk/Shalimar+Hall.php

My .htaccess file:-

 <IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^([^/]*)\.php$ /specific_hall.php?hall_name=$1&hall_id=1 [L]
   </IfModule>

I am still getting the original URL.. Can anybody please point out that why rewrite rule is not working? I tried a lot with no success. Thanks in Advance.

Upvotes: 1

Views: 106

Answers (1)

Justin Iurman
Justin Iurman

Reputation: 19026

You need to include hall_id param in your new url format in order to rewrite it back. Otherwise, mod_rewrite has no way to know it.

You can put this code in your root htaccess

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} \s/specific_hall\.php\?hall_name=([^\s&]+)&hall_id=(\d+)\s [NC]
RewriteRule ^ %2-%1.php? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\d+)-([^/]+)\.php$ specific_hall.php?hall_name=$2&hall_id=$1 [L]

Example:

  1. http://example.com/specific_hall.php?hall_name=Shalimar+Hall&hall_id=1 will redirect to http://example.com/1-Shalimar+Hall.php (no need to change links in your files).
  2. http://example.com/1-Shalimar+Hall.php will internally rewrite to (= display the same content as) http://example.com/specific_hall.php?hall_name=Shalimar+Hall&hall_id=1

Upvotes: 1

Related Questions