Htaccess Block Direct access php file - apache

I have added htaccess rules to rewrite url like following...
RewriteRule ^demo/$ demo.php [L,QSA]
This is working fine for me.
Now issue is i want to block url for demo.php file. Like user can access only
www.example.com/demo/ (valid)
www.example.com/demo.php (not valid and want to block this url)
Please help me do this.
Thanks,

You can use this rule:
RewriteCond %{THE_REQUEST} /demo\.php [NC]
RewriteRule ^ - [L,R=404]

Related

htaccess rewrite root url to subfolder with accessing other folder?

I have following folder structure in apache server.
Public_html
-->admin
--->admin_login.php
-->website
--->index.php
since the index.php inside the website folder,
i have given following code in .htaccess
RewriteCond %{REQUEST_URI} !^/website/
RewriteRule ^(.*)$ /website/$1 [L,NC]
so that the when the user enter root url , it will appear "www.myurl.com" instead of "www.myurl.com/website/"
but the issue is, i could not be able to access admin_login.php.
is there anyway to modify .htaccess, to come website/index.php in main url and able to access admin_login.php(both)?
Thanks in Advance
You need to add an exception to existing rule like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(website|admin)/ [NC]
RewriteRule .* website/$0 [L]
Negative condition %{REQUEST_URI} !^/(website|admin)/ will match every URI except URIs that start with /website/ or /admin/. This will allow you to directly open www.myurl.com/admin/admin_login.php.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/?$ website/index.php [QSA,NC,L]

Redirect URL (including port) with params using .htaccess

I am trying to redirect from
http://www.example.com:81/my/api/search?query=test
to
http://www.example.com:81/my/php/api.php?query=test
using
RewriteCond %{QUERY_STRING} ^query=(.*)$
RewriteRule ^api/search(.*)$ /php/api.php?%1 [L]
However, it doesn't work for me. Additionaly for testing htaccess rules I use http://htaccess.madewithlove.be/.
Possibly even better would be to check if request starts from ^api and it's GET type (I guess using RewriteCond %{REQUEST_METHOD}) then redirect to /my/php/api.php?[query params here]
Anyone can point me into right direction?
If your .htaccess is located in /my/ directory then you can use:
RewriteEngine On
RewriteBase /my/
RewriteCond %{QUERY_STRING} ^query=.
RewriteRule ^api/search/?$ php/api.php [L]
QUERY_STRING will be automatically carried over to target URL.

How to use htaccess Rewrite rule for just part of a url

I am trying to create a rewrite rule in my httacess file for part of the url. I want to rewrite the url if the request contains a specific string. For example change the url only if contains /members/
So
mydomain.com/members/
mydomain.com/members/activity/ros1...
mydomain.com/members/ay/bd...
ALL above should change to another url because matches /members/ string in the url
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET /members/(.*)
RewriteRule ^(.*)$ - [F,L]
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/members(.*)
RewriteRule ^(.*)$ - [F,L]
I have tried various combinations but does not seem to work. I'm sure I'm doing something really wrong as not on expert on this. Appreciate any pointers.
RewriteCond %{REQUEST_URI} ^/members
RewriteRule ^(.*)$ http://www.google.com/$1 [R, 301]
Would be something i would try to redirect a request for /members and send them to another domain. (with the same request uri). Further info on mod_rewrite can be found https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
You are using the F flag in your rewrites. The flag is to send the Forbidden status back to client. If you want to redirect the user to another URL, you'll need to pass that URL as the second parameter to RewriteRule directive with the R flag:
RewriteEngine On
RewriteRule ^members\b http://some-other-website.com [R]

Proper htaccess rewrite

I'm trying to force an old URL to go to the new url and my code seems to have no effect
RewriteCond %{QUERY_STRING} ^index2\.php?page=shop\.product_details&\.tabs\.tpl&product=310&category=71&Itemid=2$
RewriteRule .* /hunting/back-packs/multi-packs/black-2.5-pack.html [R=301,L]
my old url is
www.mywebsite.com/index2.php?page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2
and my new one is
www.mywebsite.com/hunting/back-packs/multi-packs/black-2.5-pack.html
My code does not break anything but does not work either
Thanks in advance
As I understand you want redirect (301 Permanent Redirect) so the URL will change in browser. This will work for this URL ONLY /index2.php?page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2:
RewriteCond %{QUERY_STRING} =page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2 [NC]
RewriteRule ^index2\.php$ http://www.mywebsite.com/hunting/back-packs/multi-packs/black-2.5-pack.html [R=301,L]
If you want internal redirect (rewrite), then use these lines:
RewriteCond %{QUERY_STRING} =page=shop.product_details&.tabs.tpl&product=310&category=71&Itemid=2 [NC]
RewriteRule ^index2\.php$ /hunting/back-packs/multi-packs/black-2.5-pack.html [L]
PLEASE NOTE:
You need to put these lines in a proper order (order of rules matters) otherwise (if you put it at the end) some another rule will rewrite it to a different URL.
This needs to be placed in .htaccess file in website root folder. For any other location you may need to modify it a bit.

htaccess rewrite

I've rebuilt a site using a CMS and I want to make the old urls point to the new pages. I'm having trouble because the old URL looks like this: ?secc=country_club. For instance, domain.com?secc=country_club.
I would like to either have a rule for each url or have it rewrite the ?secc=country-club to just country-club
This is what I have tried, without any success:
RewriteRule ^secc-([^-]*)$ /?secc=$1 [L]
I think it has something to do with the ? in the url
Also if it helps, I am using joomla and I do have sh404sef.
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^secc=(.+)$
RewriteRule ^(.*) %1? [R,L]
This will redirect http://example.com/?secc=MYPAGE to http://example.com/MYPAGE
I think you meant to write '=' after ^secc:
RewriteEngine on
RewriteRule ^?secc=(.*)$ "$1" [QSA]