Forum Moderators: phranque

Message Too Old, No Replies

301 Redirect for Old Subdomain Urls

         

ivanvias

4:11 pm on Sep 24, 2014 (gmt 0)

10+ Year Member



Hi,

How could I create some htaccess rules to redirect my old urls

eg. http://firstnamelastename.example.com/biography

to http://firstnamelastename.example.com/

and

http://firstnamelastename.example.com/announcements

to

http://firstnamelastename.example.com/

[edited by: Ocean10000 at 5:44 pm (utc) on Sep 24, 2014]
[edit reason] Examplified. [/edit]

phranque

6:10 pm on Sep 24, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you could achieve that using mod_rewrite - a single RewriteRule with a pattern that matches those specific paths.

however, if you are serving multiple virtual hostnames on that server configuration you'll need a preceding RewriteCond directive to exclude other hostname requests.

ivanvias

7:02 pm on Sep 24, 2014 (gmt 0)

10+ Year Member



I tried this but no luck

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^/?announcements/(.*)$
RewriteRule ^([A-Za-z0-9]+)$ https://$1.example.com/? [L,R=301]

phranque

10:39 pm on Sep 24, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



RewriteCond %{QUERY_STRING} ^/?announcements/(.*)$

"announcements", etc are not in the query string, they are in the url path.
also, using (.*)$ in this application unnecessary since you aren't using the capture group in your RewriteRule.

RewriteRule ^([A-Za-z0-9]+)$ https://$1.example.com/? [L,R=301]

this is where you should be examining the url path for "announcements", etc.
however, from your examples in the OP i don't see where the "firstnamelastname" exists in the url path so not sure where your subdomain name can be captured from the request.

ivanvias

2:57 pm on Sep 25, 2014 (gmt 0)

10+ Year Member



For firstname lastname I am using wildcard subdomains so it can be any a-z 0-9.

Basically eg.

I want to permanent redirect http://timwillis.example.com/announcements and
http://timwillis.example.com/biography to just

[timwillis.example.com...]