badcode
badcode

Reputation: 3

How do I block a part of an url with htaccess?

So basically, I have a link looking like:

mydomain.com/file.php?id=m03u7dp255jiobi&type=mp3

How do I block access to this URL only for this ?id= part

So when users visit this link file.php?id=test it won’t work, but if they visit the other link looking like this ?id=validurl it will work.

Upvotes: 0

Views: 116

Answers (1)

Steven
Steven

Reputation: 6148

.htaccess

To do this in .htaccess you can use the mod_rewrite module and set conditions against the query string.

ReWriteCond %{QUERY_STRING} id=invalid_string [OR]
ReWriteCond %{QUERY_STRING} id=another_invalid_string
ReWriteRule . /new_destination.php [QSD,L,R=307]
  • OR - Literally an OR operator; as in Condition1 || Condition 2
  • QSD - Removes the original query string
  • L - Stops further rewrite rules from being applied
  • R=307 - Sets a 307 Temporary Redirect status code
    • R=401 Sets an Unauthorised status code and redirects automatically to a predefined 401 resource (ErrorDocument)
      • You can custom set the ErrorDocuments like:

        ErrorDocument 401 /errors/unauthorised.php
        

Upvotes: 2

Related Questions