Home > Mobile >  .htaccess rule match part of new url and use as query string for old url
.htaccess rule match part of new url and use as query string for old url

Time:05-28

I am trying to match part of a URL and then use the matched expression to append onto the end of a query string from the old URL.

I have the following line in .htaccess, once I've worked out what I'm doing wrong I'll be able to fix the rest so for now I will just focus on the following line:

RewriteRule ^league/([^/]*)$/matches/? index.php?page_id=1074&league=$1

I would like ([^/]*)$ to appear where $1 is

So essentially: /league/29/matches/ would point to index.php?page=1074&league=29

Can anyone please tell me what I am doing wrong? :)

CodePudding user response:

RewriteRule ^league/([^/]*)$/matches/? index.php?page_id=1074&league=$1

The $ in ([^/]*)$ does not make sense here and will cause the regex to fail. You should also be using the quantifier here (1 or more). You are also missing the end-of-string anchor ($) at the end of the regex (otherwise the trailing /? is superfluous). Although you shouldn't really make the trailing slash optional on the rewrite as it potentially opens you up for duplicate content. (You should redirect to append/remove the trailing slash to canonicalise the URL instead.) You are also missing the L flag on the RewriteRule.

Try the following instead:

RewriteRule ^league/([^/] )/matches/?$ index.php?page_id=1074&league=$1 [L]

Although, if you are expecting digits only (as in your example) then you should be matching digits, not anything. So, the following is perhaps "more" correct:

RewriteRule ^league/(\d )/matches/$ index.php?page_id=1074&league=$1 [L]
  • Related