butteff
butteff

Reputation: 125

How to rewrite url and redirect with apache mod rewrite?

I have got url:

ipaddress/panelname/main/index.php

How to rebuild it to

ipaddress/center/index.php

?

ofcourse we can see another pages, not only index.php, but this folders in url we can see forever.

I tryed to do this in .htaccess:

RewriteEngine on
RewriteRule ^center/([^/]+)/?$ panelname/main/$1 [L]
RewriteRule ^/panelname(.*)$ /center$1 [QSA,L,R=301,NC]
Redirect 301 ^/panelname(.*)$ /center$1

but i don't see redirect from panelname to center. but if i type center all works good (but i don't shure, that it works good by my htaccess or by symlink, which i was created in filesystem)

How to rewrite all to another links and howto see redirect from old links to my new? Thank you.

Upvotes: 0

Views: 65

Answers (2)

anubhava
anubhava

Reputation: 785721

If you want to show /center/index.php to your visitors and keep a redirect from old URL to this URL then you will need one redirect and one rewrite rule (that you already have).

RewriteEngine on

# external redirect from old URL to new one
RewriteCond %{THE_REQUEST} /panelname/main/(\S+) [NC]
RewriteRule ^ /center/%1 [R=302,L]

# internal forward from new URL to actual one
RewriteRule ^center/([^/]+)/?$ panelname/main/$1 [L]

Upvotes: 0

Sumurai8
Sumurai8

Reputation: 20745

RewriteRule in directory context (which .htaccess is), does never begin with a slash, because the common prefix is stripped from the matched portion first.

Redirect does match strings, not regex'es. The variant that works on a regex is RedirectMatch. Both only work on absolute URL's (the one beginning with a slash).

You either have to do the following:

RewriteRule ^panelname(.*)$ /center$1 [R,L]

or:

RedirectMatch 302 ^/panelname(.*)$ /center$1

Change [R] to [R=301] once you have tested that EVERYTHING works. If you choose the second option, only change 302 to 301 after testing that everything works.

Upvotes: 0

Related Questions