SetEnv for a dynamic URL - .htaccess

what I am trying to do is to use SetEnv for a dynamic URL.
In example for index.php I do this
<IfModule somemodule.c>
<Files index.php>
SetEnv somevariable
</Files>
</IfModule>
However If I wanted to do this for index.php?something=123 then what should I put in my .htaccess? Will it work with SetEnvIf request_URI somehow cause I couldn't get it to work. Another thing I think could be the solution is to use rewrite_mod but I have no idea how to do it. Thanks in advance for any help!

You can use mod_rewrite's E flag to set environment variables:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^something=123$
RewriteRule ^index\.php$ - [L,E=somevariable]

Related

error in htaccess for redirecting url in php

i have following code but redirection is not working plz help
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^products/([A-Za-z0-9-]+)/?$ product.php?name=$1 [L,NC]
</IfModule>
it is not reditrecting to product.php
I modifyed the Link now and think this should work.
I tested it on my local server and it works.
RewriteRule ^([/products]+)/(.*)$ product.php?name=$1&%{QUERY_STRING} [L]
On my installation, i work on a sub dir so it can be that the param should be $2 instead of $1
It have also an impact if you use
href"hello.com....."
or
href="http://hello.com....."

Apache mod_rewrite not working

I have a problem with the mod_rewrite module for Apache 2.2
My code will neither work on localhost (Wamp on Win8 Pro, IPv6) nor on the webhotel (site5.com).
My goal is to generate SEO friendly URLs as:
www.xy.com/featured-artists.html
instead of:
www.xy.com/index.php?pageID=Artists
The PHP variable $pageID is defined on line 1 in my index.php:
<?php isset($_GET['pageID']) ? $pageID = $_GET['pageID'] : $pageID = 'Forside';?>
The code for the rewrite in my .htaccess file looks as follows:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([^/.]+)/?$ /index.php?page=$1
</IfModule>
Unfortunately that doesn't have any effect on the site. The .htaccess file is working properly though, since other sections like
ErrorDocument 404 /index.php?pageID=404
work as expected.
No matter what I do - it won't work. So I really hope that some of you can help me with that.
Thanks!
Mod_rewrite doesn't seem to be turned on. Your rules are inclosed in a IfModule container:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([^/.]+)/?$ /index.php?page=$1
</IfModule>
So the rules aren't going to get executed. You need to turn on mod rewrite in server config (or your webhost) if you want any of your rules to work. Otherwise, it doesn't matter what you try.
Once you had mod_rewrite loaded, your rules should do close to what you want. It'll take /something and internally rewrite it to /index.php?page=somethin.

deny access to index.php but allow index.php?var=val

I wonder if I can deny accessing http://domain.tld/index.php and allow only http://domain.tld/index.php?var=val
Is this possible with a rewritecond and rewriterule? I cannot figure this out. Any help?
Thanks!
Yes it is possible. The trick to use a condition to check the query string, using this in your docroot .htaccess file which allows you to forbid access to index.php unless the variable var is set:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !\bvar=
RewriteRule index.php - [F]
Does it have to be a rewrite rule? If it's a PHP script, you could detect the absence of the required parameters and just return an appropriate HTTP header to deny access.

Removing a folder name from a url using mod rewrite

I have a url structure that looks like this:
http://www.blahblah.com/community/i/pagename
However, I want to remove the ugly /i/ part. Is this possible with Modrewrite?
Yes, it's possible:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(\w+)/(\w+)$ $1/i/$2 [QSA]
</IfModule>

Turning this string in modwrite

pre_config_step_2.php?type=Grocery&count=2
to
/pre_config_step_2/Grocery/2
do i need to change the htaccess in the root directory and if so what do i change it to
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^pre_config_step_2/(\w+)/(\d+)$ pre_config_step_2.php?type=$1&count=$2
</IfModule>
That would consider accessing /pre_config_step_2/Grocery/2 as if requesting pre_config_step_2.php?type=Grocery&count=2.

Resources