I have my website and I wants something like this:
filename is: apple_banana.php
Now I wants to access this file either with underscore "apple_banana.php" or with dash "apple-banana.php" both way. but filename will stay "apple_banana.php". I don't want 2 files. so how can I do this with htaccess?
I wants this for my whole project files.
thanks, Aj
Place this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^(apple)-(banana\.php)$ $1_$2 [L,NC]
To make this generic (apply to all the URLs) and for any number of dash:
RewriteRule ^([^-]*)-+(.*)$ $1_$2 [L]
UPDATE: as per comments:
# To externally redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(apple/apple)_(banana\.php)[\s?] [NC]
RewriteRule ^ /%1-%2 [R=301,L,NE]
# internal rewrite
RewriteRule ^(apple/apple)-(banana\.php)$ $1_$2 [L,NC]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With