Search code examples
apache.htaccesshttp-redirectmod-rewriteurl-rewriting

Redirection in htaccess not working with 2 rules


htaccess working fine with first line but not working on second line. if I remove first line than second line works fine. Why both lines not working together?

RewriteRule ^(.*) /single-services.php?slug=$1
RewriteRule ^blog-(.*)$ single-blog.php?slug=$1

Solution

  • Simply change their order of execution in your file. Since your first rule covers .* which is a greedy match so that's why its executing always the first one. Since we changed the sequence of rules now now when request comes for URI starts with blog it will NOT be served by .* rule.

    Also I have added necessary flags to your rules which will help in smooth execution of rules. Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    RewriteRule ^blog-(.*)$ single-blog.php?slug=$1 [NC,QSA,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/?$ single-services.php?slug=$1 [QSA,L]
    

    NOTE: Also I have removed / before your rewriting to your php file(on right side of rules) in case 3 of your files(single-blog.php, single-services.php and .htaccess) are present in root folder then you can add / before single-blog.php and single-services.php in both Rules.