Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mod Rewrite file.php? to string

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?

like image 757
MacEncrypted Avatar asked Oct 31 '22 22:10

MacEncrypted


1 Answers

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]
like image 178
Justin Iurman Avatar answered Nov 08 '22 08:11

Justin Iurman