Alper Aydingül
Alper Aydingül

Reputation: 261

mod_rewrite: url rewrite and redirect

I have the following URL:

http://example.com/pages/cms/impressum.php

and want to get an URL like this:

http://example.com/impressum

My rewrite Rule is:

RewriteEngine on 
RewriteBase /
RewriteRule ^impressum$ /pages/cms/impressum.php [R,L]

The Problem is, I can open the url in both ways. I would like a forwarding from /pages/cms/impressum.php to /impressum. If I use [R=301], the second URL which I do not want works. I want to reverse this rule.

Upvotes: 0

Views: 42

Answers (1)

Croises
Croises

Reputation: 18671

Use:

RewriteEngine On
RewriteBase /

# To externally redirect pages/cms/impressum.php to impressum
RewriteCond %{THE_REQUEST} \s/+pages/cms/impressum\.php[\s?] [NC]
RewriteRule ^ /impressum [R=301,L]

# To internally forward impressum to pages/cms/impressum.php
RewriteRule ^impressum/?$ pages/cms/impressum.php [L,NC]

Upvotes: 1

Related Questions