Problem with conflicts - mod_rewrite

Options +FollowSymLinks -MultiViews
RewriteEngine On 

ErrorDocument 404 /404.php

RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]

RewriteRule ^(.*)/ page.php?slug=$1 [L,QSA]

I have some kind of a “conflict” with the code above in my htaccess-file.
When i add the rule to have all page.php?slug=$pagename to domain.com/$pagename, the other rewritten page URLs breaks.

I’m not completely familiar with mod_rewrite yet, but I’m trying to understand.
I would be extremely grateful if someone could help me with this.

Thanks in advance…

The issue you’re experiencing could be due to the order of the rewrite rules in your .htaccess file. The order of the rules is important because the first rule that matches the incoming URL will be executed, and subsequent rules may not be evaluated.

To resolve the conflict, you can try reordering the rules in your .htaccess file. Here’s a modified version of your code with the rules reordered:

Options +FollowSymLinks -MultiViews
ErrorDocument 404 /404.php

RewriteEngine On 

RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=301,L]

RewriteRule ^([^/]+)/?$ page.php?slug=$1 [L,QSA]

In this updated version, the rule for redirecting the domain from non-www to www is placed before the rule for rewriting page URLs. This ensures that the domain redirect is handled first, and then the page URL rewriting takes place.

The last rule RewriteRule ^([^/]+)/?$ page.php?slug=$1 [L,QSA] is modified to capture any non-empty slug in the URL and rewrite it to page.php?slug=$1. This rule should match URLs like domain.com/pagename and rewrite them internally to page.php?slug=pagename.

Make sure to replace domain.com with your actual domain name in the code.

With this reordering of rules, the conflict between the page URL rewriting and other rules should be resolved. However, keep in mind that other factors, such as server configuration or conflicting rules outside of the provided code, could also impact the behavior.

Sponsor our Newsletter | Privacy Policy | Terms of Service