Grow your CSS skills. Land your dream job.

Help with rewrite rule

  • # August 30, 2013 at 3:25 am

    I want to redirect http://www.something.com/helloworld.pdf to http://www.something.com/index.php?file=helloworld.pdf and then echoing the name of the file in index.php.

    So far I got this code:
    RewriteEngine on
    RewriteRule ([^/.]+)/?.(?i:jpg|gif|png|pdf|zip|rar|doc|docx|mov|ai|psd|swf|key)$ index.php?file=$1 [L]

    They only thing it’s missing is the extension of the file on the echo. What am I doing wrong?

    __
    # August 30, 2013 at 6:59 am

    They only thing it’s missing is the extension of the file on the echo. What am I doing wrong?

    You only passed the first subpattern ($1) to your rewritten url. The file extension is inside $2.

    Also note that ([^/.]+)/?.<– this dot matches “any character,” and so might cause patterns to match/not match that you might not otherwise expect. You can do \. instead to make it a literal “dot.”

    # August 30, 2013 at 7:08 am

    So I changed to:

    RewriteEngine on
    RewriteRule ([^/.]+)/?.(?i:jpg|gif|png|pdf|zip|rar|doc|docx|mov|ai|psd|swf|key)$ index.php?file=$1.$2 [L]

    And it’s printing [filename]. what means that the extension is not being saved in $2.

    Any idea?

    __
    # August 30, 2013 at 8:23 am

    What’s the (?i: part supposed to do? I’ve never seen that. It seems that you really only need an optional group (i.e., (this|that|other)) for this task…? That ? might be causing the pattern to be non-capturing.

    # August 30, 2013 at 8:52 am

    Nevermind. I was able to do what I wanted to:

    RewriteEngine on

    images

    RewriteRule ^(.+).jpg$ index.php?file=$1.jpg [L]
    RewriteRule ^(.+).gif$ index.php?file=$1.gif [L]
    RewriteRule ^(.+).png$ index.php?file=$1.png [L]

    docs

    RewriteRule ^(.+).pdf$ index.php?file=$1.pdf [L]
    RewriteRule ^(.+).doc$ index.php?file=$1.doc [L]

    I can add only the files I want to behave like this. :)

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".