techworld
techworld

Reputation: 331

Basic htaccess code not working

This is really frustrating for me that I have been trying to get this very basic htaccess rewrite working from hours long but couldn't do it.

I have tried following.

Options +FollowSymlinks
RewriteEngine On
RewriteBase /funshoppie/

############ Details page ################
RewriteCond %{THE_REQUEST} Deal\-details\.php\?title\=([A-Za-z0-9-]+)&id=(\d+) [NC]
RewriteRule ^%1%2? [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^Deals/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ Deal-details.php?title=$1&id=$2 [NC,QSA,L]

can anyone tell me what wrong i am doing?

url i am trying to rewrite

http://localhost/funshoppie/Deal-details.php?title=Styling%20Tools%20-%2020%%20off&id=7

what i wish after success

http://localhost/funshoppie/Deals/Styling-Tools-20%-off/7

I have checked error logs, httpconf & ensured uncommented rewrite_module. Don't know whats causing this error.

Upvotes: 6

Views: 271

Answers (2)

techworld
techworld

Reputation: 331

Finally i figured it out by my own i believe.

There was some trailing slashes issues in the last rewrite rule due to it was not doing the intended. Further i rewritten all existing & future urls via some clean url function & used those pretty statements in url. Then it worked !!

RewriteCond %{THE_REQUEST} Deal\-details\.php\?title\=([A-Za-z0-9-_]+)&id\=(\d+) [NC]
RewriteRule ^ %1/%2? [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-_]+)/(\d+)$ Deal-details.php?title=$1&id=$2 [NC,QSA,L]

This worked for me.

Upvotes: -1

Hash
Hash

Reputation: 4715

I cannot try this at the moment but, your regular expression is not fitting the URL:

Deal-details.php?title=Styling%20Tools%20-%2020%%20off&id=7

Based on en- and decoding this could be the following two string:

  • Deal-details.php?title=Styling%20Tools%20-%2020%%20off&id=7
  • Deal-details.php?title=Styling Tools - 20% off&id=7

In none of the cases matches your expression:

[..] Deal-details.php\?title\=([A-Za-z0-9-]+)&id=(\d+)[..]

You are missing represented characters from your expressions like:

  • "%"
  • " " (space)

Try the following expression:

Deal\-details\.php\?title\=(.+)&id=(\d+)

This will match any characterafter title. Otherwise I see no problem there.

Upvotes: 5

Related Questions