1

I'm trying to rewrite the file

view-forums.php?page=community-forums&section=2846

I have made a rewrite row in my .htaccess file and it's the following:

RewriteRule ^forums/(.*)/(.*) view-forums.php?page=$1&section=$2 The url should then be http://localhost/forums/community-forums/2846 but it doesn't work when I visit it. It sends me to an error page instead, any ideas?

My full htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)/(.*) index.php?page=$1&id=$2

RewriteRule ^forums/(.*)/(.*) view-forums.php?page=$1&section=$2

RewriteRule ^media$ media.php

RewriteRule ^about-us$ about.php

RewriteRule ^forums$ main-forums.php
nicael
  • 18,550
  • 13
  • 57
  • 90
Synyster
  • 41
  • 5
  • What "error page" in particular? And `^(.*)/(.*)` will likely match almost everything before all your other rules. – deceze Aug 06 '16 at 14:37
  • @deceze It sends me to a 404 error page. – Synyster Aug 06 '16 at 14:45
  • Also, when i changed the order of the index RewriteRule and the forums one, it broke the css if that's of any help. – Synyster Aug 06 '16 at 14:46
  • That would be because `^(.*)/(.*)` now matches *everything*, because the `RewriteCond` rules are individual for each `RewriteRule`. Currently your `RewriteCond` rules apply only to the first following `RewriteRule`. If you move your `RewriteRule`s around, the `RewriteCond` will apply to some other rule, and `^(.*)/(.*)` now also matches your CSS files. – deceze Aug 06 '16 at 14:52
  • @deceze Thanks, it works, though if i want to add two such as the following `RewriteRule ^forums\/(.*?)\/(.*) view-forums.php?page=$1&view=$2 RewriteRule ^forums\/(.*?)\/(.*) view-forums.php?page=$1&section=$2` How would that work? Right now, the one that is at the top is the only one that is working – Synyster Aug 06 '16 at 15:52

1 Answers1

0

You should make the regex lazy, otherwise the first capture group "eats" the second one. Try also escaping /.

RewriteRule ^forums\/(.*?)\/(.*) view-forums.php?page=$1&section=$2
nicael
  • 18,550
  • 13
  • 57
  • 90