htaccess rewrite url contains - .htaccess

I'm trying to block some urls in my site, for example i want to "ban" urls and give them 403 when contains words like options, k2 or component. The problem is that htaccess not work properly
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^(option|k2|component) [NC]
RewriteRule .* - [F]
</IfModule>
Is blocking when bot attack www.example.com/option=?asdasdad
but its not blocking when bot is attacking www.example.com/index.php/option=?asdad
Anyone?

Remove the above and Put only the following code :
RewriteEngine on
RewriteRule (option|k2|component) - [F]

Related

htaccess rewrite rule with param before filename

I'm not able to get my rewriting rules to work. I've read tutorials but nothing explains clearly how i can use a param before my filename when i'm not calling the root index. It seems that my index rule overides all the other ones.
I want my index to show galleries according to specific tags, so all kind of links looking this way would go to index.php
www.mawebsite.com/en/tag/karate = www.mawebsite.com?lang=en&tag=karate
this part is working fine and my gallery load as i wish. But then i want to be able to use other files.
www.mawebsite.com/en/video/videoid/video-title = www.mawebsite.com/video.php?lang=en&guid=videoid&title=video-title.
Here how my htaccess file looks so far.
EDIT my file:
AddCharset UTF-8 .html
ErrorDocument 404 /notfound.html
RewriteBase /
RewriteEngine on
RewriteRule ^(\w\w)/(video)/([^/]+)/([^/]+)/?$ /$2.php?lang=$1&guid=$3&title=$4 [L]
RewriteRule ^fr/video/guid/title$ video.php [L]
RewriteRule fr/tag/([^/]+/.+)$ /?=$1 [L,QSA]
#RewriteRule ^([^/]+)$ index.php?p=$1 [L]
Redirect 301 /css/file.css /css/file.php
anything i tried going to video.php won't work.
Give the following rules a try:
RewriteEngine On
RewriteRule ^(\w\w)/(video)/([^/]+)/([^/]+)/?$ /$2.php?lang=$1&guid=$3&title=$4 [L]
The ordering of rewrite rules plays a very important part. Keep the more specific rules first:
AddCharset UTF-8 .html
ErrorDocument 404 /notfound.html
Redirect 301 /css/file.css /css/file.php
RewriteBase /
RewriteEngine on
RewriteRule ^([^/]+)/?$ index.php?p=$1 [L]
RewriteRule ^(\w\w)/(tag)/([^/]+)/?$ /?lang=$1&$2=$3 [L,QSA]
RewriteRule ^(\w\w)/(video)/([^/]+)/([^/]+)/?$ /$2.php?lang=$1&guid=$3&title=$4 [L]

Redirect url without changing the url

I know there are a lot of answers available for this but none of them worked for me. I am stuck with this very familiar issue. I have made a website wherein i have to make customised urls for all my franchisees. But all these customised urls should take back to homepage.
Eg. if franchisee enters following url : www.example.com/franchisee/john it should redirect the franchisee to www.example.com but the browser url should remain www.example.com/franchisee/john
I have tried this by modifying .htaccess file but it shows page not found(404) error. Any help will be appreciable. I am new to .htaccess.
Here is .htaccess code :
RewriteCond %{REQUEST_URI} ^.*/franchisee/[a-zA-Z0-9_\.\-]+$
RewriteRule ^(.*)$ / [P]
EDIT :
Here is the complete .htaccess file. This file is present under the docroot folder:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^ - [E=protossl]
RewriteCond %{HTTPS} on
RewriteRule ^ - [E=protossl:s]
Options +FollowSymLinks -MultiViews
RewriteCond %{REQUEST_URI} ^/franchisee/[a-zA-Z0-9_\.\-]+$
RewriteRule ^franchisee/[\w.-]+/?$ / [L]
</IfModule>
You don't need to use proxy or P flag here. Use this rule without R flag to silently rewrite to /:
RewriteEngine On
RewriteRule ^franchisee/[\w.-]+/?$ / [L]

RewriteRule in .htaccess is not working

I have a site where I need to show one URL for SEO reasons, but the actual landing page is a slightly different URL. I originally thought the requirement was to show the url which actually exists, but I was incorrect.
The required url: www.somesite.com/people/johndoe/?id=10
The url which actually exists: www.somesite.com/people/?id=10
I am trying this in my .htaccess file but to no avail:
RewriteRule ^/person/.+$ /person/$1 [R=301.L]
This doesn't appear to change anything and I am told my url doesn't exist.
I have looked up many similar questions on this site and elsewhere, but cannot find a solution that works. Thanks in advance
Edit (existing .htaccess):
# php_flag magic_quotes_gpc Off
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
#AuthName "username"
#AuthName "username"
#AuthUserFile "/home/something/.htpasswds/public_html/passwd"
#require valid-user
AuthName "username"
AuthUserFile "/home/something/.htpasswds/public_html/passwd"
RewriteCond %{HTTP_HOST} ^sitename.org$
RewriteRule ^(.*)$ "http\:\/\/www\.sitename\.org\/$1" [R=301,L]
For doing internal rewrites use mod_rewrite rules with using R flag:
Use this code in your root .htaccess:
RewriteEngine On
RewriteRule ^(people)/.+$ /$1 [L,NC]
Your rewriterule condition cannot include a leading /. It is stripped off. Use
RewriteRule ^person/.+$ /person/$1 [R=301,L]
Also note that you had a period in the flags, not a comma.
Lose the " and \'s in the last rewrite rule:
RewriteRule ^(.*)$ http://www.sitename.org/$1 [R=301,L]
I don't know if you're going to get in any trouble with multiple 301s in action. It may depend upon the server configuration. Do you need for the revised URI to go back to the visitor and be seen in their browser? If not, lose the 301 on that rewrite rule.
Document what you're doing and why, with comments. For example, your last two lines are to force www.sitename.org instead of sitename.org.

mod_rewrite keep rewriting recursilvely

I just want to append the url with home but it is happening in recursion and i get the browser saying too many redirects
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteRule ^test main.html
RewriteRule rest/(.*)$ /home/rest/$1 [R=301]
</IfModule>
so i want
localhost/rest/abc.php
to
localhost/home/rest/abc.php
Remove the slash before home.
RewriteRule ^rest/(.*)$ home/rest/$1 [R=301]

Redirect all pages except one page to sub-domain htaccess

I have a index.php in my main domain root
domain.com/index.php
And ive moved my forums which was in the same "root" to a subdomain
forums.domain.com
I need to Redirect everything except the index.php
ive tryed loads of ways and none seem to work
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !index\.php$
RewriteRule ^(.*)$ http://forums.domain.com [L,R]
RewriteEngine On
RewriteCond %{HTTP_HOST} animelon\.com [NC]
RewriteCond %{REQUEST_URI} !^index\.php$
RewriteRule ^(.*)$ http://forums.domain.com/$1 [R=301,L]
If anyone has any ideas that would be great
as for the above codes I would them googling about.
Cheers
You may use RedirectMatch instead of rewriting, that is, replace all the rewrite block you are showing with:
RedirectMatch ^(/(?!index\.php).*) http://forums.domain.com$1
You can see the full explanation of the regex on Regexr here. In brief, it sends all the URIs NOT beginning with /index.php to forums.domain.com.
If you don't need any other rewrite rule, you can turn off rewriting by removing all the lines beginning with "Rewrite" from your .htaccess.

Resources