phpgetmethod

RewriteRule apply on particular directory


This is the code used in .htaccess,

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /blog/index.php?a=$1 [NC,L]

I want to apply it on a particular directory, I can't apply this rule, but currently it redirect not found urls of whole website to domain/blog/index.php


Solution

  • Use RewriteCond. When ever you want to apply a Rule to a specific condition.

    For example,

    # Include in the next line all folders to include
    RewriteCond %{REQUEST_URI}  (folder1|folder2|folder3) [NC]
    RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /blog/index.php?a=$1 [NC,L]
    

    or the other way around by adding a !

    # Include in the next line all folders to exclude
    RewriteCond %{REQUEST_URI}  !(folder1|folder2|folder3) [NC]
    RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ /blog/index.php?a=$1 [NC,L]
    

    Also, keep in mind that in some cases Wordpress overights the htaccess file

    In some cases adding the custom stuff to a block prevents that but not always. It depends on what plugins are in use.

    ## BEGIN - My Custom Redirects
    <IfModule mod_rewrite.c>
    .....
    </IfModule>
    ## END - My Custom Redirects