I need to redirect from http://mysite/component/users to main page
i tried this rule in .htaccess but code not working.
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule http://mysite/component/users/ index.php [F]
but i need to keep from redirecting this page
http://mysite/component/users/?view=reset
it's possible ?
There's a couple of things fundamentally wrong with your RewriteRule:
When you use the F flag you can't rewrite the URL. The substitution is ignored.
The RewriteRule pattern is a regular expression and matches against the URL-path, not the absolute URL.
In order to return a 403 and display the appropriate content, you could either:
Internally rewrite to index.php and conditionally return a 403 Forbidden header.
or
Serve your 403 error document (using the F flag) and conditionally show your content?
However, I would simply serve the appropriate 403 error document and link to the homepage. The request is after all forbidden....
RewriteCond %{QUERY_STRING} !^view=reset
RewriteRule ^component/users/ - [F]
The above sends a 403 to all requests for URLs that start /component/users/, excluding those that have the query string view=reset.
Related
I have a custom 403 page that works when I want to block specific pages, but it doesn't work when I want to match a specific HTTP_REFERER.
With the specific HTTP_REFERER I get the regular 403, To test the HTTP_REFERER I added a link on another site "mysite.com" towards this project, when I click on the link I get the server 403 response:
But I open my test page "forbidden-test" I do get my customized forbidden.php page
This is what I have in the htaccess, form the following example only RewriteRule ^forbidden-test$ - [F] works by showing my customized 403 page:
Options All -Indexes
# prevent folder listing
IndexIgnore *
RewriteEngine on
RewriteCond %{HTTP_REFERER} \
... (there are many here)
mysite\.com|\
[L,NC]
RewriteRule .* - [F]
#spam blacklist end
RewriteCond %{HTTP_USER_AGENT} \
12soso|\
192\.comagent|\
1noonbot|\
1on1searchbot|\
3de\_search2|\
3d\_search|\
3g\ bot|\
... (there are many here)
zyte\
[NC]
RewriteRule .* - [F]
#bad bots end
#Forbidden Test
RewriteRule ^forbidden-test$ - [F]
#ERRORS
RewriteRule ^forbidden/?$ forbidden.php [L,QSA]
ErrorDocument 403 /forbidden
Any thoughts?
Thanks
Pay attention to what that default error message is actually saying:
Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
Access to your custom error document is blocked. The internal request for that, goes through all of the rewriting again; and because the referrer of the original request is (still) wrong, access to your 403 document is forbidden now.
You need to add an exception to this referrer check, so that it allows access to your error document.
Easiest way here is probably to just put a rule at the very top, to do nothing, when this particular document is requested:
RewriteRule ^forbidden\.php$ - [L]
The - is a “non-rewrite”, it simply does nothing. The [L] flag is important here, to say “don’t process any other rules in this round.”
Also, since your error document seems to be a PHP script, you should define it like this directly,
ErrorDocument 403 /forbidden.php
Otherwise, this needs an extra round of rewriting, from /forbidden to /forbidden.php, and there is really no good reason for that.
I need to remove a parameter in some cases from URL.
The working url's are:
mywebpage.com/es/apartments/ or mywebpage.com/es/apartments/apartment1/
But some time I need to pass a value and get url like this:
mywebpage.com/es/apartments/¶m=12345/
In this cases, the page are redirected to 404 page.
The code that I have in htaccess refered to this for redirect from other page is:
RewriteCond %{QUERY_STRING} ^luxury-apartments/$
RewriteRule ^error404.php$ /es/apartments/? [L,R=301]
How I can remove this param and don't get a 404 redirect? I try some codes, but always redirect me to 404 page.
I would like to redirect in htaccess
https://www.example.com/albumgallery.php?id=8
to https://www.example.com/example-gallery
i've tried :
RewriteCond %{QUERY_STRING} ^id=8$
RewriteRule ^albumgallery.php$ https://www.example.com/example-gallery? [L,R=301]
-- it works , but on page it says
Not Found The requested URL /example-gallery was not found on this
server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
any ideas what is wrong with that ?
You are getting a 404 not found error because www.example.com/example-gallery does not exist on your server. You need to rewrite this url to your existing url. for example when you type
https://www.example.com/albumgallery.php?id=8 into your browser your rule redirects it to https://www.example.com/example-gallery since this new url doesn't exist so your server returns a 404 error status. You need to add an internal RewriteRule to your htaccess to handle your new url.
Put the following right bellow your existing Rule
RewriteRule ^example-gallery/?$ /albumgallery.php?id=8&loop=no [L]
I added an additional perameter loop=no to the Rule`s destination it's to prevent infinite loop error so that the rule can not conflict with your existing RewriteRule.
I have a Drupal site where new content is added very rarely. Recently, there have been an increasing number of visits from bots to various URLs (node/add, user/register), which return Drupal's "Access denied" page.
I want to block access to these URLs in .htaccess. I have tried the following and it works:
<IfModule mod_alias.c>
Redirect 403 /node/add
Redirect 403 /user/register
</IfModule>
However, bots can still access ?q=node/add and ?q=user/register. I have tried including ?q= in the code above but no success.
How do I block access to these URLs in .htaccess ?
You can use mod_rewrite to do url-manipulation based on the query string. You'll need something like the (untested) code below.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^q=(node/add|user/register)$
RewriteRule ^ - [F,L]
What does this do? It matches any url (^), then checks if the query string is equal to q=node/add or q=user/register. If either one matches, then the url is not rewritten (-), but access is denied [F] and the rewriting stops for this iteration [L].
While doing this via .htaccess is completely viable, I would reconsider this approach and consider putting these URLs into robots.txt for crawler bots. This way, they will ignore them completely, which is definitely more healthy for SEO.
Also, you can use Global Redirect module (https://drupal.org/project/globalredirect) to ensure that only clean URLs are used.
A quick question. I want to use a php file that will deliver pages based on the url. I am using htacess to deal with the error docs but my question is to do with SEO. If I redirect a 404/403 request to pageDispacher.php that then delivers the correct page will the header be a 404/403 at any point? As this is not cool. Do I need to use a rewrite rule instead?
In which case is there is a genuine 404 page how would i return a 404 header?
www.example.com > www.example.com/en/home
www.example.com/en/ > www.example.com/en/home
ErrorDocument 404 /pageDispacher.php
ErrorDocument 403 /pageDispacher.php
This is usable:
RewriteRule ^error error.php [L,QSA]
RewriteRule ^(.*)\.php$ error[L,QSA]