rokkz
rokkz

Reputation: 179

htaccesss rewrite won't work

I need some help rewriting the following url's:

example.com/news/top/2/

example.com/news/top/

This is my current .htaccess file

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+) index.php?view=$1&task=$2
RewriteRule ^news/([^/\.]+)/([0-9]+) index.php?view=news&task=$1&page=$2 [L]
RewriteRule ^news index.php?view=news

I thought this would work but whenever I visit = example.com/news/top/2/ and I try to echo out the $_GET['page'] it says that the variable is not defined.

Can somone pleas help with this nerve breaking problem?

Upvotes: 1

Views: 56

Answers (2)

Jon Lin
Jon Lin

Reputation: 143856

This rule:

RewriteRule ^([^/\.]+)/([^/\.]+) index.php?view=$1&task=$2

needs a $:

RewriteRule ^([^/\.]+)/([^/\.]+)$ index.php?view=$1&task=$2

because it is matching beyond just /dir1/dir2

Upvotes: 1

anubhava
anubhava

Reputation: 784898

Make sure to use end anchor $

RewriteEngine on

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^news/([^/.]+)/([0-9]+)/?$ index.php?view=news&task=$1&page=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/?$ index.php?view=$1&task=$2 [L]
RewriteRule ^news/?$ index.php?view=news [L]

Upvotes: 2

Related Questions