Home > database >  How to prevent rewrite .htaccess file conflict problem
How to prevent rewrite .htaccess file conflict problem

Time:06-23

RewriteEngine on

RewriteRule ^([^/\.] )/?$ category.php?slug=$1
RewriteRule ^([^/\.] )/([^/\.] )$ product-details.php?slug1=$1&slug=$2

RewriteRule ^([^/\.] )/?$ infrastructure-details.php?slug=$1

what I have already tried This is my htaccess file. problem is when I am trying to execute (infrastructure-details.php?slug=$1) its move to (category.php?slug=$1) conflict with first rule of htaccess.

I tired multiple rewrite methods but its not working. Please help to solve this issue.

CodePudding user response:

localhost/project/category.php?slug=pump, localhost/project/infrastructure-details.php?slug=paint second url i want to be-> localhost/project/paint both page is different. can you please specify how to write rules for this different pages.

There is no discernible pattern that differentiates these two URLs so the only way to implement these two rewrites is to hardcode them. For example:

RewriteRule ^pump$ category.php?slug=$0 [L]
RewriteRule ^paint$ infrastructure-details.php?slug=$0 [L]

Where the $0 backreference in the substitution string contains the entire match by the RewriteRule pattern (just saves some repetition).

If you need a more general solution (as your directives suggest) then there needs to be a discernible pattern in the URL that differentiates URLs that should be rewritten to category.php and infrastructure-details.php respectively.

I'm assuming your .htaccess file, and other files, are is inside the /project subdirectory.

RewriteRule ^([^/\.] )/?$ category.php?slug=$1
RewriteRule ^([^/\.] )/([^/\.] )$ product-details.php?slug1=$1&slug=$2

RewriteRule ^([^/\.] )/?$ infrastructure-details.php?slug=$1

Rule #1 and #3 conflict - they use exactly the same pattern (regex) to match against the requested URL. The first rule is always going to "win" and rewrite the request before rule#3 is able to process the request, so rule#3 never matches.

To write a generic rule like this there needs to be a discernible difference between the URL types that you can match with a pattern/regex. For example:

/category/pump
/infrastructure/paint

And then you can construct rules...

Options -MultiViews

RewriteRule ^category/([^/] )$ category.php?slug=$1 [L]
RewriteRule ^infrastructure/([^/] )$ infrastructure-details.php?slug=$1 [L]

Note that the order of these directives can be important. More specific rules need to be before more generalised rules.

CodePudding user response:

Options -MultiViews
RewriteEngine on
RewriteRule ^infrastructure/([^/] )$ infrastructure-details.php?slug=$1 [L]
RewriteRule ^([^/\.] )/?$ category.php?slug=$1 [L]
RewriteRule ^([^/\.] )/([^/\.] )$ product-details.php?slug1=$1&slug=$2 [L]

This is work fine for me. (infrastructure-details.php?slug=$1 [L]) put on top.

  • Related