5

I have old site with links like this:

http://domain.com/image.php?690

And I would like to change it into:

http://domain.com/old690

I have tried many different Rules, fe.:

RewriteRule ^image.php?(.+) old$1 [L]

EDIT: All rules looks like:

RewriteEngine On

RewriteRule ^admin/(.*) ninja-admin/$1 [L]

RewriteRule ^index.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php [L]

RewriteRule ^image.php\?(.+) old$1 [L]

What is correct RewriteRule, and why?

2 Answers 2

1

You're doing it upside down.

Put this code in your htaccess

RewriteEngine On
RewriteBase /

RewriteRule ^old([0-9]+)$ image.php?$1 [L]
RewriteRule ^admin/(.*)$ ninja-admin/$1 [L]
RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]


The rule for your images is the following

RewriteRule ^old([0-9]+)$ image.php?$1 [L]

It will forward every url like /old123 (where 123 is one or more digits) to image.php?123


EDIT: if you want to forbid direct access to image.php?xxx then you can do it this way

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} \s/image\.php\?([0-9]+) [NC]
RewriteRule ^ old%1 [R=301,L]

RewriteRule ^old([0-9]+)$ image.php?$1 [L]
RewriteRule ^admin/(.*)$ ninja-admin/$1 [L]
RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
3
  • Dear Justin. I would like to forward /image.php?123 to /old123. So the new link should be /old123 Jun 7, 2014 at 16:23
  • Well if your new link is /old123 then it's this one that is forwarded to /image.php?123 and not the opposite. I think you're doing some mix with it Jun 7, 2014 at 19:53
  • 1
    OK. Your suggestion work from /old123 to /image.php?123 but no other way. I figgured out that '?' need to be escaped but \ didnt work. Finally i do that with .php code: $keys = array_keys($_GET); header("HTTP/1.1 301 Moved Permanently"); header("Location: http:/domain.com/old".$keys[0]); Jun 8, 2014 at 17:39
1

I use this so http://example.com/old4444 forwards to http://example.com/image.php?file=4444.

Also, the browser will keep http://example.com/old4444 in the address bar.

RewriteCond %{QUERY_STRING} !marker  
RewriteCond %{QUERY_STRING} file=([0-9]+)
RewriteRule ^/?image.php$ %1? [R=301,L]
RewriteRule ^/?old([-a-zA-Z0-9_+]+)$ image.php?marker&file=$1 [L]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.