سؤال

I am trying to redirect a URL that looks like this: http://getdotastats.com/sig/28755155.png

To one that looks like this: http://getdotastats.com/sig/?aid=28755155

The first URL being the imaginary one that does not exist.

The regex I cam up with is below. Can you see how to improve it. I lack the necessary know how in forming the regex, so I just made it match groups, and ignore the ones I don't need.

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)sig/(.*)\.(.*)$ index.php?aid=$2 [NC,L]

Can I just throw the above in a .htaccess in the specific directory (/sigs), or are the additional directives required in the main vhost file?

Thanks for helping!

EDIT: Thanks futuretelematics. Using the .htaccess below:

RewriteEngine On
RewriteBase /
RewriteRule testpage\.html http://www.google.com [R]
RewriteRule ^/sig/([0-9]+\.(?:gif|png|jpg))$ index.php?aid=$1 [NC,L]

The testpage redirect to google, but the image redirect throws the following error.

The requested URL /sig/28755155.png was not found on this server.

We must be close. _

Vhost looks like:

    DocumentRoot /home/www-dota2
    <Directory /home/www-dota2/>
            Options Indexes MultiViews +SymLinksIfOwnerMatch
            AllowOverride All
            Order allow,deny
            allow from all
    </Directory>
هل كانت مفيدة؟

المحلول

First of all, I'd recommend you to write more specific regex: To match /sig/28755155.png, I'd do:

RewriteRule ^/sig/([0-9]+\.(?:gif|png|jpg))$ index.php?aid=$1 [NC,L]

Bear in mind that your regex ^(.)sig/(.).(.*)$ could match paths like /somesig/foo/bar/123.png

Aswering to your question:

  • YES, you can put this rules in a htaccess file of your sig folder
  • YES the rules in htaccess in sig folder are computed for any subfolder (ej: /sig/foo/bar)

Hope this helps

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top