0

I'm currently working on a project powered by a home-made CMS and I'm experiencing some issues with URL rewriting.

Here's the thing: all the website is centralized around the index.php located in the main directory. Depending on what he gets thought the URL, the index.php displays the right page (the pages are included from a inc/pages/ folder)

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?page=$1 [NC]

For a single parameter, it works great. http://demo.com/subscribe/ or demo.com/subscribe does transmit a $_GET['page'] to the index.

For some pages, I do need a second parameter. So it's not required for each single pages. Per example, http://demo.com/edit/I-love-Stackoverflow should transmit a $_GET['snd_param')

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index.php?page=$1&snd_param=$2 [NC]

I tried this but this isn't working well. First, if the second parameter is not mentioned (demo.com/edit) it's not working. The index doesn't receive the right $_GET['page']. Secondly, when the second parameter is mentionned, it "works" but apache believes this is a directory. My index page is then located in the fictive "I-love-Stackoverflow" folder and loading the CSS, images and javascript fails.

I hope I explained my issue pretty clearly ! Thanks in advance for helping me

TheJals
  • 73
  • 1
  • 6

1 Answers1

0

You should treat the rules separately. All Conditions preceding rules only apply to a single rule, so basically the second RewriteRule is not executed at all.

You can use something like this:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?page=$1 [L]

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

My index page is then located in the fictive "I-love-Stackoverflow" folder and loading the CSS, images and javascript fails.

You are probably load your assets using relative paths, so the browser only knows for the unmodified url ( http://demo.com/edit/I-love-Stackoverflow ) in your case, and the wrong urls are created when browser load the assets. If you load resources with absolute paths instead of relative, you will be okay.

Kristian Vitozev
  • 5,791
  • 6
  • 36
  • 56
  • Thanks a lot for your help. It worked perfectly. I added extra /? before the ending $ to allow both urls (with or without final /) to work ! I'll now fix this matter of absolute and relative paths. Do you think it is possible to solve this still using relative paths, and creating an additional rewriteRule ? Like, when there are two parameters, the assets/ folder is rewrote to match the pathways ? – TheJals May 24 '15 at 20:27
  • SOLVED - I fixed the issue using html tag :) Thanks for your help ! – TheJals May 24 '15 at 22:24
  • Please check this post: http://stackoverflow.com/questions/1889076/is-it-recommended-to-use-the-base-html-tag/1889898#1889898 – Kristian Vitozev May 25 '15 at 07:00
  • Thanks again vitozev, that was really instructive. I'll figure out another way if I experience such problems ! – TheJals May 26 '15 at 14:15