Jason Paddle
Jason Paddle

Reputation: 1103

URL rewrite error

I trying to rewrite url

site.com/videos?cat=1&page=2

to this

site.com/videos/cat-1/page-2

and using this:

RewriteRule ^videos/cat-([0-9]+)/page-([0-9]+)/?$ videos?cat=$1&page=$2
RewriteRule ^videos/cat-([0-9]+)/?$ videos?cat=$1

I get error #500 (Internal Server Error). What can be wrong?

Upvotes: 1

Views: 79

Answers (2)

anubhava
anubhava

Reputation: 785216

Most likely you're causing infinite looping in Apache mod_rewrite and that's causing 500.

Replace your code with this:

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

# to redirect /videos?cat=1&page=2 => /videos/cat-1/page-2
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(videos)\?cat=([^&]+)&page=([^\s]+) [NC]
RewriteRule ^ /%1/cat-%2/page-%3? [R=302,L,NE]

RewriteCond %{QUERY_STRING} !^.+$ [NC]
RewriteRule ^videos/cat-([0-9]+)/?$ /videos?cat=$1 [L,QSA,NC]

# to forward /videos/cat-1/page-2 => /videos?cat=1&page=2
RewriteCond %{QUERY_STRING} !^.+$ [NC]
RewriteRule ^videos/cat-([0-9]+)/page-([0-9]+)/?$ /videos?cat=$1&page=$2 [L,QSA,NC]

Upvotes: 3

Adam Hopkinson
Adam Hopkinson

Reputation: 28795

That looks correct, but what you're trying to do is actually the other way round - you're trying to rewrite the input url:

site.com/videos/cat-1/page-2

To:

site.com/videos?cat=1&page=2

EDIT You've written your rule to redirect from videos/cat-1/page-2 to videos?cat=1&page=2. While this is your choice, if you're writing a rule for SEO purposes or for cleaner urls then the url you want people to enter is videos/cat-1/page-2 - in which case, the rule you have will work fine.

If you want people to enter the messier url, then @anubhava's answer is perfect for you.

Upvotes: 3

Related Questions