Mobile app version of vmapp.org
Login or Join
Ravi8258870

: .htaccess URL rewriting friendly URL with 2 parameters, the second parameter is optional I'm kind of stuck at this part and was hoping that I'd get some assistance. I'm building a highscores

@Ravi8258870

Posted in: #Htaccess #Seo #UrlRewriting

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=&page= [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= [L]

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Ravi8258870

1 Comments

Sorted by latest first Latest Oldest Best

 

@Alves908

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

RewriteRule ^highscores/skill/([^/]+)(/([^/]+))?$ /highscores/skills.php?skill=&page= [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 (the parenthesised sub-sub pattern), rather than (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= [L]



(d+) Checks for digits only (ie. it can only be a page number)
I've also removed the . prefix to the substitution path.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme