Unsuccessful attempt using mod rewrite

I have a URL like: h*tp://example.com/?mod=news&action=view&title=an-example-of-title&cat=2&id=334
URL that I want: h*tp://example.com/an-example-of-title-2-334

My .htaccess line:

RewriteEngine On
RewriteRule ^(.*)-(.*)-(.*)$ ./?mod=news&action=view&title=$1&cid=$2&page_id=$3

But it goes to 404 page.

If the URL write like: h*tp://example.com/article-an-example-of-title-2-334

With .htaccess line:

RewriteEngine On
RewriteRule ^artikel-(.*)-(.*)-(.*)$ ./?mod=news&action=view&title=$1&cid=$2&page_id=$3

It will successfully goes to h*tp://example.com/article-an-example-of-title-2-334

Please someone direct me where did I do wrong? Thank you.

NB: please replace h*tp with http, since new user like me can’t put more than 2 links in a post.

Hi @w4r0,

Do you have any other rules in your .htaccess file?

One suggestion would be to put [L] at the end of the RewriteRule, so it doesn’t process any other rules in your .htaccess file that may also match.

Another would be to make your RewriteRule less greedy, using (so the last two captures must be numbers)

RewriteRule ^(.*)-([0-9]+)-([0-9]+)$ ./?mod=news&action=view&title=$1&cid=$2&page_id=$3

First, thanks for your answer, I already use your suggestion without success.
Here’s myy complete .htaccess:

RewriteEngine On
RewriteRule ^(.*)-(.*)-(.*)$ ./?mod=main&title=$1&cid=$2&page_id=$3

RewriteRule info_c_(.*)\.htm$ ./?mod=news&cid=$1
RewriteRule (.*)-([0-9]+)$ ./?mod=news&title=$1&cid=$2
*RewriteRule info_(.*)_c_(.*)_p_(.*)\.htm$ ./?mod=news&action=$1&cid=$2&page_id=$3*
**RewriteRule ^(.*)-([0-9]+)-([0-9]+)$ ./?mod=news&action=view&title=$1&cid=$2&page_id=$3 [L]**

The italic line is an old rule but still use it for search engine sake, but my client wants the bold line.

Okay, so you have conflicting rules.

You have this rule:

RewriteRule ^(.*)-(.*)-(.*)$ ./?mod=main&title=$1&cid=$2&page_id=$3

which conflicts with your new rule

RewriteRule ^(.*)-([0-9]+)-([0-9]+)$ ./?mod=news&action=view&title=$1&cid=$2&page_id=$3 [L]

So you need to be able to separate them in some sort of way. Can you tell me more about the mod=main rule and what it is intended to catch? And how what it catches would differ from your mod=news rule?

Also, this rule may be a conflict too

RewriteRule (.*)-([0-9]+)$ ./?mod=news&title=$1&cid=$2

I see that conflicting rule now… :frowning:

mod=main & mod=news are coming from different tables, that’s why I write different modules.

Ok, I can’t separate them unless I’m using different prefixes for each rules, which is back to old rule that I wrote before:

RewriteRule ^article-(.*)-([0-9]+)-([0-9]+)$ ./?mod=main&title=$1&cid=$2&page_id=$3
RewriteRule ^news-(.*)-([0-9]+)-([0-9]+)$ ./?mod=news&action=view&title=$1&cid=$2&page_id=$3 [L]

Thank you for pointing me to a clearer direction.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.