htaccess deny access to URIs via regular expression - .htaccess

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

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]

Related

htaccess to append a query string to all URLs

I need to append /?test=1 to every single URL on the website.
My current solution that is not working:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^$ /?test=1 [L,NE,R=301]
Can someone please tell me what I'm doing wrong.
What you ask in your question contradicts partly what you show in your attempt. So this is just a guess, more leaning to what you actually ask:
RewriteEngine on
RewriteCond %{QUERY_STRING} !(?:^|&)test=1(?:&|$)
RewriteCond ^ %{REQUEST_URI}?test=1 [QSA,END]

Restrict link with htaccess file

I need to restrict access to any link that contains the word user.
My rewrite rule looks like this
RewriteRule (^|/)user(/|$) - [F,L]
It works fine when http://sitename.com/user is used but does not work for http://sitename.com/?q=user
Can anyone suggest a fix for this ?
Thanks in advance
You'll have to check the request uri and the query string separately like this:
RewriteEngine On
RewriteCond %{REMOTE_HOST} !^123\.456\.789\.123$
RewriteCond %{REQUEST_URI} ^/user$ [OR]
RewriteCond %{QUERY_STRING} ^q=user$
RewriteRule ^(.*)$ / [R=302,L]

.htaccess allow if url contains a word

I want to allow access to specific domains. For example if domain contains the word asdf it should allow access. I final attempt before asking was:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^.*asdf.*$ [NC,OR]
RewriteCond %{HTTP_REFERER} !^.*1234.*$
#RewriteRule .* - [F]
So here I tried to restrict access to all but domains that contain asdf or 1234.
You need to use %{HTTP_HOST} for checking the domain in URL instead of %{HTTP_REFERER}.
Can you try this code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^.*(asdf|1234)\. [NC]
RewriteRule .* - [F]
Anubhava gave me a clue but not with the http_host. Finally the problem was the OR.
Now the following worked like a charm:
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^.*(1234|asdf).* [NC]
RewriteRule .* - [F]
So HTTP_REFERER did what it should do (check the domain accessing). And the | worked as the or argument I needed.

htaccess rule..not able to solve

i am struggling to get my site htaccess work... but no luck..
below is the code
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/? /profile.php?username=$1 [L,NC]
</IfModule>
this works fine no issues.. but when i add something it will not work.. in following cases -
1) For example when i type example.com/dinesh it will redirect me to www.example.com (always home page instead i want it to be www.example.com/dinesh
2) now i have another users.php with two parameters the thing is when i pass one parameter it should execute this rule profile.php but when i pass two parameters then it should take me to user.php i tried so many combination but this is not working.
if any expert can give me atleast some tips that will be great.
As for 1), it probably doesn't have anything to do with the above rules. If /dinesh exists, you should look in there. If it doesn't exist, you should look into /profile.php, that's probably what's redirecting you to the home page.
As for 2), your rule:
RewriteRule ^([^/]+)/? /profile.php?username=$1 [L,NC]
Matches the URI: /something/else, because the regular expression doesn't have an end-of-string match. It matches the first something, and that's good enough. If you add a $ to the end, it won't match /something/else.
RewriteRule ^([^/]+)/?$ /profile.php?username=$1 [L,NC]
Or, you can place the other rule that routes to user.php before the one that routes to profile. but it's better to have the $.

.htaccess mod_rewrite playing with variables

I want to have my site urls look like
http://example.com/place/info?var=info&morevars=ifneeded
Place and info are also variables but they have a fixed name, the ones after would vary. EDIT This is the url I am trying to rewrite
http://example.com/test.php?place=test&action=info&var=info&morevars=ifneeded
This is what I have so far
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ test.php?place=$1 [NC]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ test.php?place=$1&action=$2 [NC]
I think there a way to do this with {QUERY_STRING} but I can't get it to work just 500 errors or it don't make a differences.
You have set the QSA flag that automatically appends the original requested query to the new one:
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ test.php?place=$1 [NC,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ test.php?place=$1&action=$2 [NC,QSA]
You're missing the first /
RewriteEngine on
RewriteRule ^/([A-Za-z0-9-]+)/ test2.php?place=$1 [NC]
RewriteRule ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/ test2.php?place=$1&action=$2 [NC]

Resources