Forum Moderators: phranque

Message Too Old, No Replies

get user supplied URL used as input to regex rather than local path?

         

jboy

12:53 pm on Dec 24, 2010 (gmt 0)

10+ Year Member



I always thought the text which the regular expressions of rewrite rules work on was the URL (minus the http-//www.whatever.com part) which the user supplies, but this:

RewriteEngine on
RewriteRule /(.*).png$1 [R=301]


shows the server local path (like ...domain.com/home/users/#*$!xx/html/....).

How can I get to have the user supplied path as the input for the regex of the RewriteRule rather than the local server path? How can I work with what the user supplied?

Thanks.

wilderness

3:02 pm on Dec 24, 2010 (gmt 0)

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



either method may be what your looking for:

REQUEST_URI

HTTP_REFERER

jboy

3:58 pm on Dec 24, 2010 (gmt 0)

10+ Year Member



Hello, thanks for the reply.

Sorry to be a thicky, but how would I add either of those two to the below to allow the regex of the RewriteRule to act on the user supplied rather than local server path?

RewriteEngine on
RewriteRule /(.*).png $1 [R=301]


Thanks.

g1smd

4:27 pm on Dec 24, 2010 (gmt 0)

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



Detect using a preceding RewriteCond and create backreferences %1, %2, etc.

Add the [L] flag to every rule.

jboy

5:53 pm on Dec 24, 2010 (gmt 0)

10+ Year Member



Thanks, but sorry, I'm still not sure how to do it I'm afraid. I've tried a number of things. The question is, what to do/add to this code:

RewriteEngine on
RewriteRule /(.*).png $1 [R=301]


in order to get it so that the (.*) part acts on what the user types into the address bar of the browser (or some part of it), rather than acting on the, what I'd call (but not sure if it's the correct name or not), server path (the ...domain.com/home/users/#*$!x/html/... kind of address).

Thanks.

jboy

7:38 pm on Dec 24, 2010 (gmt 0)

10+ Year Member



Where else could I find out how to do this?

jboy

7:48 pm on Dec 24, 2010 (gmt 0)

10+ Year Member



This is just hopeless. Why is this stuff (mod rewrite) by far the worst aspect of computers, web programming etc.? I just despise it.

wilderness

8:02 pm on Dec 24, 2010 (gmt 0)

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



Where else could I find out how to do this?


One beginning might be to learn how to search (SE of your choice) at specific websites content for a specific term?

Using google, search www.webmasterworld for Santa Claus [google.com]

(note the above method may vary from SE to SE).

Using google, search www.webmasterworld for "REQUEST_URI" [google.com]

Hope this example helps.

jboy

8:09 pm on Dec 24, 2010 (gmt 0)

10+ Year Member



I've got it at last:

RewriteEngine On
RewriteCond %{REQUEST_URI} .*
RewriteRule (.*) $1 [R=301,L]


I've got no idea what the %1, %2's were about. Just seemed to give internal server errors.

Not fun. Thanks.

jboy

8:20 pm on Dec 24, 2010 (gmt 0)

10+ Year Member



> Hope this example helps.

Two links to Google's home page was not of any use to me, no. An example, something actually demonstrating literally how to do it -- exactly like what I posted in the end -- would have been *way*, and I really mean *massively*, more helpful. Seeing as it's text which goes in both .htaccess files and forum posts, it seems much better to post an actual example rather than describing it. There was talk of %1's etc. I have no idea if that was a mistake, if I should be using those... bah. Thanks anyway.

jboy

8:34 pm on Dec 24, 2010 (gmt 0)

10+ Year Member



Sorry but this rewrite stuff drives me potty.

jdMorgan

4:08 pm on Jan 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One reason it's difficult is that you have not described exactly what it is that your code is intended to accomplish. The following questions need to be answered to get meaningful help:

  • What URL-paths do you want to redirect?
  • What similar URL-paths do you *not* want to redirect?
  • Where do you want to redirect the requested URL-paths to?
  • What parts of the client-requested URL-paths that you wish to redirect are fixed, and what parts are variable? (In other words, what parts of the client-requested URL-path need to be changed, and what parts need to be kept and copied to the new URL?)

    If you do not understand the $1 and $2 back-references, take a look at the mod_rewrite documentation at apache.org. Because mod_rewrite and regular expressions are totally unforgiving of even tiny errors, there is essentially zero probability that you will be able to design and implement correct and robust code without reading and understanding the mod_rewrite documentation.

    Your code above has incorrect syntax, so all I can suggest by guessing at the currently-undefined goal is that

    RewriteEngine on
    #
    # Redirect to remove .png file extensions from URL-paths at any directory level
    RewriteRule ^(([^/]+/)*[^.]+)\.png$ http://www.example.com/$1 [R=301,L]

    would be the correct and more-efficient equivalent to the code originally posted just above.

    Note that if this function is what is actually desired, and if those extensionless URLs are later to be rewritten back to .png filepaths, then a more-complex solution will be needed to prevent an 'infinite' redirect-rewrite loop:

    RewriteEngine on
    #
    # Redirect to remove .png file extensions from client-requested URL-paths at any directory level
    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.png([?#][^\ ]*)?\ HTTP/
    RewriteRule ^(([^/]+/)*[^.]+)\.png$ http://www.example.com/$1 [R=301,L]

    Jim
  •