Deepthi Khandelwal
Deepthi Khandelwal

Reputation: 13

My .htaccess rule is not working

I want all my URL with matching pattern as

/released/2013/iron-man 
/released/2013/abc-of-death 
/released/2012/saw6

to be redirected to

/released/1.php

and I can the name as well.

I am adding this rule to my .htaccess file but its not working

RewriteRule ^released/([0-9]+)/?$ /released/1.php?id=$1 [L]

Upvotes: 1

Views: 65

Answers (3)

anubhava
anubhava

Reputation: 785256

Problem is that you have $ after second slash but you have movie name after 2nd slash like iron-man etc. Remove $ since you are not matching it.

Make sure that mod_rewrite and .htaccess are enabled through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(released)/([0-9]+)/ /$1/1.php?id=$2 [L,QSA,NC]

Upvotes: 1

Joe Johnson
Joe Johnson

Reputation: 1824

The trailing question mark matches an optional ending / which is not what you want.

^released/([0-9]+)/iron-man$

or

RewriteRule ^released/([0-9]+)/(.+)$ /released/1.php?id=$1+$2

Upvotes: 1

Niels Keurentjes
Niels Keurentjes

Reputation: 41958

A RewriteRule which does not specify a specific external domain is always executed internally. To make it redirect as you ask add [R=301] at the end - or 302, 303 or 307 depending on which kind of redirect you require, but usually 301 is fine.

Besides that, the regular expression you wrote does not allow for extended URLs - remove the trailing $. After that the /? part is moot so you can remove it as well.

The resulting line would read:

RewriteRule ^released/([0-9]+) /released/1.php?id=$1 [L,R=301]

Upvotes: 0

Related Questions