Search code examples
.htaccesshttp-redirectmod-rewriteurl-rewritingfriendly-url

htaccess deny access to URIs via regular expression


I want to deny access to all my posts/pages via their database id address e.g. /?p=1 , /?p=2 , ... etc

I am looking at doing this using .htaccess and would guess it would be best done with a RewriteCond rule. I have tried this but it doesn't appear to work.

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/?p=*
RewriteRule ^(.*)$ - [F,L]

Any help greatly appreciated


Solution

  • With your shown samples, please try following. Please make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    RewriteCond %{QUERY_STRING} ^p=[0-9]+ [NC]
    RewriteRule ^ - [F,L]
    

    As per OP's comments to match p or page with = digits try following then.

    RewriteEngine ON
    RewriteCond %{QUERY_STRING} ^(p|page)=[0-9]+ [NC]
    RewriteRule ^ - [F,L]