NarolaInfotech Demo
NarolaInfotech Demo

Reputation: 75

htaccess file run with dash or with underscore both

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

Upvotes: 3

Views: 494

Answers (1)

anubhava
anubhava

Reputation: 785651

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]

Upvotes: 2

Related Questions