Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess redirect to file

I have to humble myself and ask for some guidance on a topic I thought I understood. I am struggling with a 301 redirect issue, and hope someone not engrossed in this issue can help me see the light.

My domain once used folders as the primary product target, ie, domain.com/prod1, domain.com/prod2, etc, but recently changed the targets to .php files, ie, domain.com/prod1.php, domain.com/prod2, etc. It seems I am having a problem since my source and targets have the same name, and I am selecting a wildcard which causes redirect loops. My normal redirects are working, but I haven't been able to find the secret sauce to redirecting these non-existant folders to their respective php files. My latest attempt is as follows:

RedirectMatch 301 ^/prod1 http://domain.com/prod1.php

Maybe it's just too late, but I could really use some help getting (me) straightened out. Thanks!

like image 542
Jimmyb Avatar asked Mar 04 '26 02:03

Jimmyb


2 Answers

You could add the end of line to your matching regular expression: ^/prod1(/)?$

like image 50
Kevin Stricker Avatar answered Mar 06 '26 01:03

Kevin Stricker


Your regular expression matches your target (^/prod1 matches /prod1.php). So you can either add a terminating character to your regexp or use mod_rewrite and a condition.

RedirectMatch 301 ^/prod1/?$ http://domain.com/prod1.php

Or:

RewriteEngine On
# request doesn't end with ".php"
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^([^/]+)/?$ http://domain.com/$1.php [R=301]
like image 37
Jon Lin Avatar answered Mar 06 '26 00:03

Jon Lin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!