1

I'm kind of stuck at this part and was hoping that I'd get some assistance. I'm building a highscores page in PHP, that's going great, it works. However, I dislike the idea of index.php?skill=name and therefore wanted a bit of SEO in this.

I have successfully replaced the url with a more friendly version:
highscores/skill/name

And this is where the problem starts, I have added pagination to the highscores and the page is read from the HTTP_GET page variable ($_GET['page']).

I dislike the idea of highscores/skill/name&page=2 and was hoping if you guys could assist me to make the url like the following:

Page 1, so accessing the file without declaring the page number:

DOMAIN.TLD/highscores/skill/name

Page > 1 so now the page variable is needed:

DOMAIN.TLD/highscores/skill/name/2

As you can tell the "2" will define page 2 and load the correct data for page 2. However, I'm having much trouble in my .htaccess file to configure it this way.

RewriteRule ^highscores\/skill\/(.*?)(\/(.*?)*)$ highscores/skills.php?skill=$1&page=$2 [L] # Skills page

That is my latest attempt in order to get it to work, unfortunately it does not work, it makes the page look horrible (CSS doesn't work) and it doesn't go to the page specified on the URL.


PART 2 - Thanks to w3d for solving my first issue, however, I now face a second issue, which I was hoping the first issue would fix for me.

My index page in the highscores directory shows the overall scores, domain.tld/highscores/. However, I don't like the idea of domain.tld/highscores/?page=2

Could you tell me how to make it like: domain.tld/highscores/2
the 2 being the page number of course.

Here's what I've tried:

RewriteRule ^highscores/([^/]+)?$ ./highscores/index.php?page=$1 [L]
0

1 Answer 1

2

Try the following in .htaccess (in the document root):

RewriteRule ^highscores/skill/([^/]+)(/([^/]+))?$ /highscores/skills.php?skill=$1&page=$3 [L]
  • I've assumed highscores/skills.php is physically located from the document root, so have made this URL root-relative in the substitution. Using relative URLs could be problematic here and may require the use of RewriteBase.

  • The page number part is now entirely optional, previously it would have failed to match if the page number was omitted. eg. DOMAIN.TLD/highscores/skill/name (will presumably need to check for "empty" or 1 in your page code.)

  • The regex matches anything other than a / (path separator) in the URL sections, rather than any character.

  • We capture $3 (the parenthesised sub-sub pattern), rather than $2 (the second parenthesised pattern that would have included the slash).

  • Don't include inline comments at the end of the statement, this can break later versions of Apache.

The non working CSS is possibly due to the use of relative paths to your CSS files? In this case the browser thinks you are either 2 or 3 folders deep (depending on whether the page number is included), however, skills.php is only 1 folder deep (from which you are probably referencing your CSS files). Use root-relative URLs instead.


PART 2 - In answer to the second part of your question... the reason for the 500 Internal Server error is that the two rules are conflicting, resulting in a rewrite-loop. You can avoid the conflict in this instance by specifically checking for a page number, rather than anything, in your second RewriteRule:

RewriteRule ^highscores/(\d+)?$ /highscores/index.php?page=$1 [L]
  • (\d+) Checks for digits only (ie. it can only be a page number)

  • I've also removed the . prefix to the substitution path.

5
  • Hey buddy, thank you for solving my issue! It works!! :) However, i was hoping with this fix I could implement it on my overall page, however it does not work. The overall page is "domain.tld/highscores/" which obviously leads to index.php. My goal is to make "domain.tld/highscores/2" which would mean the overall scores is at page 2, but when I try, i get internal 500 error. here's the code: RewriteRule ^highscores/([^/]+)?$ ./highscores/index.php?page=$1 [L] Hope you can help, thank you! I'll edit the main question too. Jun 7, 2014 at 14:26
  • ^^^ - Sorry forgot to tag your name above. @w3d Jun 7, 2014 at 14:34
  • I've updated my answer with "part 2" of your question. (You don't actually need to tag the name when directing a comment to the answerer - since the answerer will get notified of all comments to their answer.)
    – MrWhite
    Jun 7, 2014 at 15:07
  • Thank you so much buddy, glad you could help me! Before I let you go (hehe) could you elaborate on the conflicting part? Becase i wana make sure i know what my errors are so for next time, i'll be able to fix it. here is my code: #Highscores directory RewriteRule ^highscores/skill/([^/]+)(/?)([^/]+)?$ /highscores/skills.php?skill=$1&page=$3 [L] RewriteRule ^highscores/user/([^/]+)$ /highscores/personal.php?user=$1 [L] RewriteRule ^highscores/(\d+)?$ /highscores/index.php?page=$1 [L] So if it would be conflicting, wouldn't the second rewrite rule cause an error 500? Thank you. Jun 7, 2014 at 15:41
  • Well, in the original directives, there were really two conflicts, one with the directive itself: /highscores/2 -> /highscores/index.php?page=2 -> /highscores/index.php?page=index.php?page=2 -> etc. Then, if you attempted to access: /highscores/skill/name/2 -> /highscores/skills.php?skill=name&page=2 -> /highscores/index.php?page=skills.php?skill=name&page=2 -> etc.
    – MrWhite
    Jun 7, 2014 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.