Welcome to WebmasterWorld Guest from 54.196.147.57

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Variables In .HTACCESS For Your Repeated Locations or Paths

Is there any solution in htaccess to variables

     

sweetguyzzz

4:32 pm on Jul 7, 2011 (gmt 0)

5+ Year Member



Hi,

I do not know that it is possible or not but I think that it could be done. Please tell me that is it possible in .htaccess to make variables or any other thing so using it I have to change my path and location in that variable and all locations in my .htaccess get values from that variable so no need to change every where. Like example given below:

If my htaccess have following code:
RewriteRule something /mysitefolder/anotherfolder/something

RewriteRule otherthing /mysitefolder/anotherfolder/otherthing

RewriteRule onemore /mysitefolder/anotherfolder/onemore

So now if I have to change my folder or path then I have to change it on every place.

But if there is any variable in place of those locations then I only have to change those variables value and every location will be changed

Thanks in Advance
Waiting for the best replies.
Thanks again

lucy24

7:34 pm on Jul 7, 2011 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Waiting for the best replies.

Picky, picky. Yes, your three examples are all the same rule. But the exact wording of the rule depends on whether you're matching all possible inputs or just a specific range of them.

The two possibilities:

RewriteRule (something|otherthing|onemore) mysitefolder/anotherfolder/$1

where the pipe | means "either this | or this | or this"

or else

RewriteRule ^(\w+) mysitefolder/anotherfolder/$1

depending on how specific you need to be. Apache says you can use \w ("word character"); if it doesn't work, replace with [a-z\d] or even [a-z0-1] with a bracketed [NC] at the end of the line. If any of the affected filenames might contain a hyphen, change to [-\w]+ or [-a-z0-9]. You can also say [^/]+ meaning "stop when you get to the first slash".

In all cases, the Rule should be followed by something in brackets unless you really intend to continue through htaccess looking for more possible rewrites. At a minimum, [L] means "stop here and don't do any more rewriting".

g1smd

8:39 pm on Jul 7, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Is the "something" in the original request the same "something" that the request is rewritten to?

If so, then captured "(pattern)" mapped to backreference "$n" is the right approach.

Do add the [L] flag to every rule. Omitting it can lead to difficult to diagnose problems.

sweetguyzzz

5:06 am on Jul 8, 2011 (gmt 0)

5+ Year Member



Hi,

Thanks lucy24 and g1smd for your best replies but I think my question is not so clear.

Above given .htaccess is an examplem, its not of my own site. But I want is that if you have too many different RewriteRules and RewriteConditions and all of them have there second parameter that is rewritten one started with the location of my website path that is "mysitefolder/anotherfolder/" so I like to put this "mysitefolder/anotherfolder/" in some variable so to get it in all places like I give an example of php code given below:

<?php
$my_var = "good";

echo "I am ".$my_var." and my friend John is also very ".$my_var." and we both have too many ".$my_var." friends";
?>


So in this above code if we change $my_var value then in all coding its gonna be change so I am thinking of same in .htaccess

Please tell me solution to this. If you did not understand question then I will try to clear more.
I will be very thankful to all of you.

Thanks in Advance
Waiting for reply.

lucy24

5:37 am on Jul 8, 2011 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



OK, got it now. You're asking about a RewriteBase [httpd.apache.org].

To me it looks as if it would do the exact opposite of what you want, but g1smd will explain how to make it work ;)

If it really does do the opposite of what you want, Option B is a non-final rewrite (that is, leave off the [L]) where you start by sending everything from

wrongdirectory/
to
rightdirectory/likethis/

and then carry on with the detailed rewrites, if any more are needed. (This may be an exception to the specific-to-general principle, because if one part of the rewrite is being done to everything, you have to get it out of the way before you start throwing [L]s around.)

g1smd

7:43 am on Jul 8, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



.htaccess is a configuration file not a program. As such, it does not use "variables". So, it is not possible to define a "constant" that you re-use again and again in the file. You can carry a backreference over from request to action, that is all.

sweetguyzzz

8:08 am on Jul 8, 2011 (gmt 0)

5+ Year Member



Ok,

Thanks for it, I understand your point. I just a have simple question but it is off-topic that is about regex.

What is regex code if I have to match with a-z 0-9 but not . or /

I tried ([a-z0-9^/.]+)

Thanks for responding.

lucy24

8:21 am on Jul 8, 2011 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



I think what he meant in the original post here:

RewriteRule something /mysitefolder/anotherfolder/something
RewriteRule otherthing /mysitefolder/anotherfolder/otherthing
RewriteRule onemore /mysitefolder/anotherfolder/onemore


was how to plug the /mysitefolder/anotherfolder into all the rewrites instead of having to type it in separately each time. It won't be called a constant, but there are ways to achieve the desired result.

This is assuming for the sake of discussion that there is always a way to make .htaccess do what you want, provided you say it in grammatically correct, idiomatic Apache ;)

:: overlapping ::

What is regex code if I have to match with a-z 0-9 but not . or /

I tried ([a-z0-9^/.]+)

You can only use ^ at the very beginning of brackets to negate the whole group. And once you have specified a-z0-9 you do not need to exclude anything; they have already been excluded. So it is either
[a-z0-9] ... [NC]
or
[^./]
The second version will also include - and _ which might legitimately occur in an url. It would also include spaces-- which should not be there, but might occur anyway-- and anything that's been encoded, because a % is neither a / nor a .

If you want to search for any one piece of an url, the [^./] form is probably better, unless you know for certain that you have nothing but alphanumerics.

[edited by: lucy24 at 8:27 am (utc) on Jul 8, 2011]

g1smd

8:21 am on Jul 8, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



[a-z0-9]
will match ONLY a-z and 0-9. NOTHING else is matched.

[^/.]
will match everything OTHER than / and . and will therefore also match a-z and 0-9 as well as ; , & ( + # @ etc.

sweetguyzzz

8:28 am on Jul 8, 2011 (gmt 0)

5+ Year Member



Ok great,

What a silly mistake I am doing.
Well thanks for the great answers
 

Featured Threads

Hot Threads This Week

Hot Threads This Month