1

I had a rule in my .htaccess that makes URLs for articles more friendly looking for the purposes of SEO and the like.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(news-and-views)/(.+).php /$1/article.php?title=$2 [L]

Which converted this URL:

/news-and-views/going-for-brokering.php

To this within the application itself:

/news-and-views/article.php?title=going-for-brokering

Now I need a URL with an ID before the title like this:

/news-and-views/123456789/going-for-brokering.php

So I tried the following rule:

RewriteRule ^(news-and-views)/(.+)/(.+).php /$1/article.php?Id=$2&title=$3 [L]

However, this isn't working, am I misunderstanding the use of the brackets as I thought everything between them was acknowledged as a variable on the right-hand side?

I'm thinking it could even be that the less specific rule is above the more specific rule.

2
  • 1
    .htaccess rules never make your URLs "friendly looking" – they act the other way around and resolve "speaking" URLs into a machine readable counterpart. Commented Aug 1, 2018 at 14:13
  • Excuse my backward logic. Commented Aug 1, 2018 at 15:58

1 Answer 1

2

You need to be careful about the order of the rules, since your first rule will also match /news-and-views/123456789/going-for-brokering.php. Change your rules as follows:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(news-and-views)/([^/]+).php /$1/article.php?title=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(news-and-views)/([0-9]+)/([^/]+).php /$1/article.php?Id=$2&title=$3 [L]
Sign up to request clarification or add additional context in comments.

2 Comments

…or reverse the order of the original rules, as stated in your text (“be careful about the order of the rules”).
Yes, but generally it's better to write more specific rules than just rely on the L flag.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.