.htaccess - Allow access only if URL matches exactly - .htaccess

I have a file (www.sample.com/sample.json).
I would like the file to be accessed only by the URL (www.sample.com/sample.json?custom-text).
If it's accessed without the custom-text, I want it to redirect to homepage or show a forbidden error.
I do not want to make any changes to my site www.sample.com. I want all the changes only to the file, sample.json (www.sample.com/sample.json)
I'm new to .htaccess, so sorry if the terms I used are wrong. Thanks in Advance.

You can use this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^custom-text$ [NC]
RewriteRule ^sample\.json$ - [F,NC]

Related

I want to use a RewriteRule in .htaccess to allow subfolders

We have a url mydomain.com/events that needs to include a country code just before th events folder like this mycomain.com/uk/events. The country could be any number of different codes. I want to use a RewriteRule like:
RewriteRule ^([a-zA-Z0-9-/]+)/events/*.* /events/*.*
so any any url like these will work:
mydomain.com/uk/events
mydomain.com/uk/events/index.php
mydomain.com/uk/events/list-events.php?s=12
With your shown samples/attempts, please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs. Place your htaccess file along with your domain folder(not inside it beside it).
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/([^/]*)/(?:[^/]*)/([^/]*)(/.*?\.php(?:\?[^=]+=\d+)?)?\s [NC]
RewriteRule ^ %1/%2%3 [L]

deny access of php file from url htaccess

Here's the Scenario,
I have a sample link that looks like this
Let's say ProjectName/products.php
I was able to rename the URL in htaccess to make it look like: ProjectName/products
HTACCESS:
RewriteEngine On
RewriteRule ^products?$ products.php
My question is, is it possible to deny access when trying to enter URL ProjectName/products.php ? I'm new in htaccess. I've tried adding deny from all, yes it solved the problem of not accessing that php file but it happens that I also can't access now the cleaned URL, I hope i made my question clear. thanks for the help.
To deny external access to the php file, you can use the following rule :
RewriteEngine on
RewriteCond %{THE_REQUEST} /products\.php [NC]
RewriteRule ^ - [R=403,L]

Redirecting URLs with multiple parameters to clean URLs in .htaccess

I'm attempting to redirect URLs in .htaccess that have multiple parameters for example:
http://www.example.com/index.php?p=product&id=10&parent=0
to a clean URL on another domain like
http://example2.com/product/product10/
I believe my normal method of redirection (using a PHP header in an index file of a directory I've created) will not work due to not being able to put ? in a directory name.
I've done some minor .htaccess but have no experience with escaping parameters or anything and the only tutorials I can find are for escaping only a single parameter.
Can anybody give me a few pointers please?
You can use that in your root .htaccess:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^p=([^&]+)&id=([^&]+) [NC]
RewriteRule ^index\.php$ http://example2.com/%1/%1%2/? [R=302]
Change [R=302] for [R=301] when test work well.

Using mod_rewrite to mask a directory/file name in a URL

I've taken my site down for some prolonged maintenance and am using mod_rewrite to send all requests to a single page: www.mysite.com/temp/503.php
This is my .htaccess file which works fine.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/temp/503.php [NC]
RewriteRule .* /temp/503.php [R,L]
However, what I'd also like to be able to do is to hide /temp/503.php in the resulting URL from the visitor.
I know this is perhaps trivial and I'm sure fairly simple to achieve, but with my limited mod_rewrite skills I can't seem to get it to work.
Any help would be much appreciated.
Thanks.
Just get rid of the R flag in the rewrite rule, which tells the rule to redirect the request, thus changing the URL in the browser's location bar. So the rule would look like:
RewriteRule .* /temp/503.php [L]
which internally rewrites the requested URI instead of externally telling the browser that it's been moved to a new URL.

htaccess Redirection - domain.com/folder to website.com/folder

I have what I thought would be a simple question but I suppose I'm missing something.
I have a website with two domain names associated with it. What I'd like to do is modify my .htaccess file so that anyone trying to access a particular folder is redirected to the correct domain name.
For example:
Someone accessing www.domain.com/folder/ should be redirected to www.website.com/folder/, but only for that folder.
What would I have to include in the .htaccess file to do this?
Any help will be appreciated!
RewriteCond will do the trick. NOTE: I didn't test this verbatim, so it's psuedocodish.
RewriteCond ${HTTP_HOST} domain.com [OR]
RewriteCond ${HTTP_HOST} www.domain.com
RewriteRule ^folder/(.*)$ http://www.website.com/folder/$1 [QSA,R=301,L]
Make sure you check the docs at http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html specifically flags like [QSA] which means to append the query string.
Note the R=301 flag, which will tell the visitor that this is a permanent redirect. There are many other flags you can use...

Resources