htaccess | apply query only to a part of the url - .htaccess

I've a rule in my .htaccess, but I want to apply it only to a specific URL, as below:
A. mysite.net/index.php?option=com_contact ** this should be blocked
B. mysite.net/admin/index.php?option=com_contact ** this should work
The rule below works fine for A, but it also blocks B.
How to block only A?
RewriteCond %{QUERY_STRING} ^option=(com_contact)$
RewriteRule ^ - [F,L]
Thanks

Related

.htaccess rewrite rule for category/subcategory

I want use rewrite rule for categories
But my code work for first category only
My code :
RewriteRule ^category/([^/]+)/$ category.php?url=$1 [NC,L]
My Test :
http://nnn.com/category/a/ -> work
http://nnn.com/category/a/b/ -> not work
http://nnn.com/category/a/b/c/ -> not work
You may change your rule to this:
RewriteRule ^category/(.+)$ category.php?url=$1 [NC,QSA,L]
.+ pattern instead of [^/]+ will allow 1+ of any characters including / in parameter url.

RewriteRule translating url to use different php files

In the .htaccess file of my PHP application I use one simple rewrite to translate my URLs from:
/shop/hats/detroit/
to:
index.php?url=/shop/hats/detroit/
using the following rewrite rule:
# Rule 1
RewriteRule ^(.*)$ /repos/nvp/httpdocs/index.php?url=/$1 [L,QSA]
I would like the following rewrite to work as well:
# Rule 2
RewriteRule ^pic/(.*)$ /repos/nvp/httpdocs/get_image.php?pic=$1 [L,QSA]
But the two rules are kind of overwriting or conflicting with each other. They work each on their own but not together. How can I use all urls to use Rule 1 and only if url starts with pic/ to use Rule 2 instead of Rule 1?
This is because the pattern (.*) matches all uris .
In order to avoid the rules overriding ,you need to reorder your rules and put the specific rules first in order.
# specific rules
RewriteRule ^pic/(.*)$ /repos/nvp/httpdocs/get_image.php?pic=$1 [L,QSA]
#catch-all rules
RewriteRule ^(.*)$ /repos/nvp/httpdocs/index.php?url=/$1 [L,QSA]

When to use [L] flag in mod_rewrite

Can someone give me an example when to use [L] flag? I'm learning about mod_rewrite moudle in .htaccess file and can't find out when to this flag.
The L flag simply means to stop applying any rules that follow. Given the same URL, http://example.com/foo/bar?q=blah, and given the rules:
RewriteRule ^foo -
RewriteCond %{REQUEST_URI} !^/bar.php
RewriteRule ^(.*)$ /bar.php?z=$1
The first rule gets applied and the URI gets passed through unchanged (via the - target). The rewrite engine then processes the next rule, and the URI gets rewritten to /bar.php?z=foo/bar. What happens when you add an L to the end:
RewriteRule ^foo - [L]
RewriteCond %{REQUEST_URI} !^/bar.php
RewriteRule ^(.*)$ /bar.php?z=$1
The URL http://example.com/foo/bar gets passed through untouched from the first rule, then stops because of the L flag. If the URL is http://example.com/something/else then the first rule doesn't match and the second rule gets applied, rewriting the URI to: /bar.php?z=something/else
Note that since the rewrite engine loops through all the rules until the URI stops changing, the L flag will not prevent the looping, only any further rules from getting applied in the current iteration.

Url Rewrite Rule not being used

I have the following rules in my htaccess file:
RewriteRule ^client-hub/roundtable/([0-9]+)$ /client-hub/roundtable?roundtable=$1 [QSA,L]
RewriteRule ^client-hub/roundtable/([0-9]+)/setup$ /client-hub/roundtable/setup?roundtable=$1 [QSA,L]
The first rule works fine, the second does not.
An expected input would be /client-hub/roundtable/123/setup
and the expected end url would be:
/client-hub/roundtable/setup?roundtable=123 (which works fine if accessed directly), but this second rule doesn't appear to work.
Any suggestions?

htaccess rewrite rule another parameter

I have this rule in my htaccess:
RewriteRule ^video_(.*)$ show-video2/?txtkey=$1 [L]
Sometimes it can happend that I need another parameter in GET, for example:
.../show-video2/?txtkey=122312421&anotherparam=1232
The problem is that with this RewriteRule i'm not able too see the second parameter in $_GET:
RewriteRule ^video_(.*)$ show-video2/?txtkey=$1 [L]
How I have to modify the RewirteRule in order to see the second parameter ( when there is it )?
Infact if a var_dump($_GET) I can only see the txtkey parameter...
Change your RewriteRule to:
RewriteRule ^video_(.*)$ show-video2/?txtkey=$1 [L,QSA]
The QSA is what you're after - it means query string append - and will let you capture the rest of your GET

Resources