Forum Moderators: phranque

Message Too Old, No Replies

Any directory redirection to one file

         

artmedia

3:51 pm on Mar 16, 2009 (gmt 0)

10+ Year Member



Hi! I need a redirection of any page in any directory that is using this query string siteid=yyyyy to grab that value as a cookie in cookie.php

Then I have created this rule:
RewriteCond %{QUERY_STRING} siteid=(.*)$
RewriteRule ^(index\.php)?$ http://example.com/cookie.php?host=%{HTTP_HOST}%{REQUEST_URI}&siteid=%1

But this is working just for index.php, because I tried
RewriteRule ^(.*)?$ http://example.com/cookie.php?host=%{HTTP_HOST}%{REQUEST_URI}&siteid=%1 but it does not work.

g1smd

4:25 pm on Mar 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



When you say 'all' files, do you really need this to redirect for robots.txt, images, css and js files too?

As coded, you have a 302 redirect. If this is a redirect, add [R=301,L] on the end of what you already have.

"Does not work" is too vague to be of any clue.

artmedia

5:47 pm on Mar 16, 2009 (gmt 0)

10+ Year Member



Ok sorry, let me be more specific, I need redirect any php, htm or html page.

for example:
1. example.com/index.php?siteid=yyy --> example.com/cookie.php?siteid=yyy
2. example.com/something.html?siteid=yyy --> example.com/cookie.php?siteid=yyy
3. example.com/other.htm?siteid=yyy --> example.com/cookie.php?siteid=yyy

And for that cases these rules are working:
RewriteCond %{QUERY_STRING} siteid=(.*)$
RewriteRule ^(.*)\.(htm¦html)$ http://example.com/cookie.php?host=%{HTTP_HOST}%{REQUEST_URI}&siteid=%1

RewriteCond %{QUERY_STRING} siteid=(.*)$
RewriteRule ^/(.*)\.php$ http://example.com/cookie.php?host=%{HTTP_HOST}%{REQUEST_URI}&siteid=%1

But when I need do it for a directoy, example:

1. example.com/dir1/index.php?siteid=yyy --> example.com/cookie.php?siteid=yyy
2. example.com/dir2/something.html?siteid=yyy --> example.com/cookie.php?siteid=yyy
3. example.com/dir3/dir4/other.htm?siteid=yyy --> example.com/cookie.php?siteid=yyy

I use this rule:
RewriteRule ^/(.*)/(.*)\.(htm¦html)$ http://example.com/cookie.php?host=%{HTTP_HOST}%{REQUEST_URI}&siteid=%1 [R=301,L]

but, it does not work, do not nothing.

Thanks!

jdMorgan

6:41 pm on Mar 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



URLs "seen" by RewriteRule in a per-directory (.htaccess) context do not include the path to the current .htaccess directory. Therefore, you must not start you pattern with a slash. You second rule above (redirect .php) would not work for this reason.

You can accommodate any number of directory-levels (including zero) and improve the efficiency of your codde like this:


RewriteCond %{QUERY_STRING} siteid=
RewriteRule ^(([^/]+/)*[^.]+\.(html?¦php))$ http://example.com/cookie.php?host=%{HTTP_HOST}$1 [QSA,L]

Jim

g1smd

7:59 pm on Mar 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



*** example.com/index.php?siteid=yyy ***

What happens if someone requests example.com/?siteid=yyy which is an 'equivalent' URL?

artmedia

9:19 pm on Mar 16, 2009 (gmt 0)

10+ Year Member



ok, i follow your suggestion, but using [QSA,L] instead of %1, it did not work, and Morgan, about the second rule, you were right, I have these two now:

RewriteCond %{QUERY_STRING} siteid=(.*)$
RewriteRule ^(.*)\.html?$ http://example.com/cookie.php?host=%{HTTP_HOST}%{REQUEST_URI}&siteid=%1

RewriteCond %{QUERY_STRING} siteid=(.*)$
RewriteRule ^(index\.php)?$ http://example.com/cookie.php?host=%{HTTP_HOST}%{REQUEST_URI}&siteid=%1

The second one, is the answer to g1smd.

I don't know exactly why this do not work using just one rule and adding the php, for example:
RewriteCond %{QUERY_STRING} siteid=(.*)$
RewriteRule ^(.*)\.(html?¦php)$ http://example.com/cookie.php?host=%{HTTP_HOST}%{REQUEST_URI}&siteid=%1

I get this error: Internal Server Error 500

But, about the directories thing, i cannot do that? redirect from any directory to one root file?

Thanks

artmedia

10:08 pm on Mar 16, 2009 (gmt 0)

10+ Year Member



ok, checking everything again, i think Jim gave me the answer with

RewriteRule ^(([^/]+/)*[^.]+\.(html?¦php))$ http://example.com/cookie.php?host=%{HTTP_HOST}$1 [QSA,L]

That is for any directory level, just was to optimal to see it the first time, anyway i can't continue if a simple rule like this:
RewriteCond %{QUERY_STRING} siteid=(.*)$
RewriteRule ^(.)+\.php$ http://example.com/cookie.php?host=%{HTTP_HOST}%{REQUEST_URI}&siteid=%1

do not redirect for any php file, even I have a rule for html and htm files working, do I have some specific configuration for php files?

Thanks again

artmedia

4:16 pm on Mar 17, 2009 (gmt 0)

10+ Year Member



well, nobody around, anyway i just want to say i fixed my problem, was close to Morgan's suggestion but with a little change:

RewriteCond %{QUERY_STRING} siteid=(.*)$
RewriteRule ^(([^/]+)/)*([^/]+)\.(html?)+$ http://example.com/cookie.php?host=%{HTTP_HOST}%{REQUEST_URI}&siteid=%1

artmedia

5:53 pm on Mar 17, 2009 (gmt 0)

10+ Year Member



I forgot the case that g1smd said, then the complete solution is:
RewriteCond %{QUERY_STRING} siteid=(.*)$
RewriteRule ^((([^/]+)/)*([^/]+)\.(html?)+)?$ http://example.com/cookie.php?host=%{HTTP_HOST}%{REQUEST_URI}&siteid=%1

Caterham

7:45 pm on Mar 17, 2009 (gmt 0)

10+ Year Member



Don't know why you're using such a regular expression if you don't need any backreferences from parts of the pattern but if your intention is to speed things up, use a possessive quantifier with a positive lookbehind (since apache 2.0)

^(?:.++(?<=\.html?))?$

g1smd

8:53 pm on Mar 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The last code gives a 302 redirect - probably not what you want.

Add [R=301,L] to the end to make it into a 301 redirect.

artmedia

10:03 pm on Mar 18, 2009 (gmt 0)

10+ Year Member



g1smd,actually i need a 302, i'll do the 301 with the php file after setting the cookie, thanks anyway.

Caterham, i tested your code, but it doesn't work, actuallt I don't undertand it, can you explain it a little bit more?

Caterham

11:23 pm on Mar 18, 2009 (gmt 0)

10+ Year Member



Mixed things up - ? is allowed for lookbehinds but not for lookaheads.

^(?:.++(?<=\.html¦\.htm))?$

You'll have to replace the ¦ pipe with solid ones since the latin1 character 7C is replaced with A6 when posting to this forum.

^(?:................)?$

^ matches at the starting position of the string
(?:....) everything inside the parenthesis is not remembered as a backerference (-> ?: = non-capturing group)
? at the end left of $ makes the expression optional (and will match an empty string in our case which is the example.com/ request)
$ matches at the end of the string

.++(?<=\.html¦\.htm)

.++ matches 1 or n of any characters without trying it again (no backtracking/re-evaluation, fails immediately)
(?<=\.html¦\.htm) .html or .htm must follow the match '.++'. This sub-pattern is evaluated prior '.++'.

g1smd

1:02 am on Mar 19, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you need a 302, then add [R=302,L] the end of the rule. The [L] is important.