Search code examples
regex.htaccessmod-rewritehttp-redirect

htaccess redirect from empty folder to another directory


I have an empty directory at www.example.com/ar/carprices.

I would like to redirect all urls from

www.example.com/ar/carprices

to

www.example.com/carprices/ar

I have placed the following .htaccess in the www.example.com/ar/carprices folder but it does not work:

RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule (.*)ar/carprices(.*) $1carprices/ar$2 [L,R=301,NC]

Solution

  • You can also use RedirectMatch here :

    RedirectMatch ^/ar/carprices/(.*)$ /carprices/ar/$1
    

    Will redirect /ar/carprices/ to /carprices/ar

    For internal url redirection ,use :

    In htaccess :

    RewriteEngine on
    
    RewriteRule ^ar/carprices/(.*)$ /carprices/ar/$1 [NC,L]
    

    leading slash is not matched in RewriteRule in htaccess context, that is why I remove it from the regex pattern.