Robin V
Robin V

Reputation: 101

htaccess RewriteRule not working anymore

Earlier today i got the following code working:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.* - [L]
RewriteRule ^(.*) index.php?p=$1

Now I tried to extend the code by changing the last line:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.* - [L]
RewriteRule ^(.*)/(.*)/(.*)/ index.php?p=$1&sub1=$2&sub3=$3

But now it doesnt work anymore, it just tries to direct me to the directory I request instead of the rewrite and then 404's because the dir ofcourse doesn't exist.

Also this:

RewriteRule ^/(.*)/(.*)/(.*)/ index.php?p=$1&sub1=$2&sub3=$3

doesn't work.

Anyone?

Thanks in advance again :)

Upvotes: 1

Views: 1182

Answers (2)

jeroen
jeroen

Reputation: 91762

Your first (.*) probably takes your whole path, causing it to look for 3 slashes at the end so you would need lazy matching to avoid that:

RewriteRule ^(.*?)/(.*?)/(.*?)/ index.php?p=$1&sub1=$2&sub3=$3

Although personally I would not blindly accept all characters but do something like (depending on your exact needs...):

RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/ index.php?p=$1&sub1=$2&sub3=$3

This would allow only word characters and the minus characters in your path names.

Upvotes: 1

rybo111
rybo111

Reputation: 12588

I recommend using:

RewriteRule ^(.*)$ index.php [L]

Then on index.php, you can use:

$p = explode("/", $_SERVER['REQUEST_URI']);

You can then access $p[1], $p[2], etc.

Upvotes: 2

Related Questions