5
votes

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

2 Answers

1
votes

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]
1
votes

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]