.htaccess redirect to file
-
21-03-2021 - |
문제
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!
해결책
You could add the end of line to your matching regular expression: ^/prod1(/)?$
다른 팁
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]