SenorAmor
SenorAmor

Reputation: 3345

.htaccess rewrite rule not working for me

I'm trying to set up some pretty URLs so

http://www.foo.com/results/2014

calls

http://www.foo.com/results/index.php?year=2014

Where '2014' could really be any string (and I will do the appropriate error-handling should I get inappropriate input).

What I'm getting, however, is that http://www.foo.com/results/ works fine but anything after the trailing slash returns a 404 error.

Below are the contents of my .htaccess file. Could someone please point out my error?

Thanks in advance!

.htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.foo.com/results/$1 [L,R=301]
RewriteRule ^results/([^/]+)/?$ /results/index.php?year=$1

Upvotes: 1

Views: 21

Answers (1)

anubhava
anubhava

Reputation: 784898

Have it this way inside /results/ directory:

RewriteEngine On
RewriteBase /results/

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

RewriteRule ^([^/.]+)/?$  index.php?year=$1 [L,QSA]

Reason why your rule didn't work:

  1. You can only match URI pattern relative from current directory. So if you're already in results/ folder and URI is /results/foo then matched URI will be foo in RewriteRule
  2. No use of RewriteBase

Upvotes: 1

Related Questions