echo_Me
echo_Me

Reputation: 37243

rewrite rule in htaccess didnt work

im trying to beautify my urls, my urls are like that

   http://www.mysite.com/details.php?id=19&object=1

object=1 (videos) or object=0 (articles)

i want change this url to

    http://www.mysite.com/videos/19

of course i make videos because i mentioned that when object =1 means videos

and when object =0

i want this

    http://www.mysite.com/articles/19

I tried using this from some tutorials , but didnt work.nothing happen.

    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^videos/([a-zA-Z]+)/([0-9]+)/$ details.php?object=$1&id=$2

and also how do i do the if condition with RewriteCond to check if object is 1 or 0 , if 1 then print videos else articles.

any help would be much apreciated.

Upvotes: 2

Views: 56

Answers (1)

anubhava
anubhava

Reputation: 786291

It is better to use RewriteMap for your case here. Here is a sample how to use it:

  1. Add following line to your httpd.conf file:

    RewriteMap objMap txt://path/to/objectMap.txt
    
  2. Create a text file as /path/to/objectMap.txt like this:

    articles 0
    videos 1
    
  3. Add these line in your .htaccess file under DOCUMENT_ROOT:

    Options +FollowSymLinks -MultiViews
    RewriteEngine on
    
    RewriteRule ^([^/]+)/([0-9]+)/?$ /details.php?object=${objMap:$1}&id=$2 [L,QSA]
    

Advantage: With this setup in place, you can edit or recreate the file /path/to/objectMap.txt anytime you have a new object id mapping without any need to add new rules.

UPDATE: If you have no control over Apache config you will have to deal with multiple rewrite rules (one each of each object id mapping) like this:

Options +FollowSymLinks -MultiViews
RewriteEngine on

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+details\.php\?id=([^&]+)&object=1\s [NC]
RewriteRule ^ /videos/%1? [R=302,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+details\.php\?id=([^&]+)&object=0\s [NC]
RewriteRule ^ /articles/%1? [R=302,L]

RewriteRule ^videos/([0-9]+)/?$ /details.php?object=1&id=$1 [L,QSA,NC]
RewriteRule ^articles/([0-9]+)/?$ /details.php?object=0&id=$1 [L,QSA,NC]

Upvotes: 1

Related Questions