Forum Moderators: phranque

Message Too Old, No Replies

Help with Redirect Rules

         

ivanvias

4:30 am on Jul 15, 2014 (gmt 0)

10+ Year Member



Hi,

I want to create a redirect in htaccess for urls that are like this:

[example.com...] or [example.com...]

To redirect to [firstnamelastname.example.com...]

lucy24

4:56 am on Jul 15, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That sounds like a good plan. What have you tried so far and what are the results?

What, if any, is the significance of the mv= parameter? If you remove it from the URL, its information will no longer be available to the page.

Incidentally there's a parallel thread asking this identical question [webmasterworld.com]. In fact, the thread creator's name is remarkably similar to yours.

ivanvias

5:13 am on Jul 15, 2014 (gmt 0)

10+ Year Member



Yes I want to redirect both instances.

ivanvias

5:16 am on Jul 15, 2014 (gmt 0)

10+ Year Member



I tried this:


RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ [$1.example.com...] [L,R=301]
RedirectMatch 301 ^([a-z0-9\-]+)/(.*)$ [example.com...]


But then when i go to main page it puts the [.example.com...]

phranque

11:31 am on Jul 15, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



RedirectMatch is a mod_alias directive.
you want to try using a mod_rewrite directive instead.

(they don't play well together.)

penders

2:12 pm on Jul 15, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As lucy24 asked, is the mv= parameter important in identifying the redirect? It's just that you make no reference to this at all in your example code?

ivanvias

4:02 pm on Jul 15, 2014 (gmt 0)

10+ Year Member



yes the mv can be 1 to 4 . So the rule should redirect all [example.com...] and eg [example.com...] to [firstnamelastname.example.com...]

lucy24

6:59 pm on Jul 15, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you are simply throwing away the mv= parameter, and there are no other parameters to look out for, then you don't need to say anything about it at all. Just make sure the target ends in ? to get rid of the query string.

I still don't understand why this question is in a new thread. We're still talking about
firstnamelastname.example.com
subdomains, right?

ivanvias

8:34 pm on Jul 15, 2014 (gmt 0)

10+ Year Member



Any experts out there to lend a hand?

ivanvias

4:24 am on Jul 17, 2014 (gmt 0)

10+ Year Member



I tried this:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^mv=[0-4]$
RewriteRule ^([A-Za-z0-9]+)$ [$1.example.com...] [L,R]


But this doesnt work for the [example.com...]

It only does [example.com...]

Can anyone else help

phranque

10:18 am on Jul 17, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



are you saying that the query string is optional for this ruleset?

ivanvias

3:19 pm on Jul 17, 2014 (gmt 0)

10+ Year Member



Primarily i want to redirect [example.com...] and then I also want to redirect [example.com...] etc

lucy24

5:44 pm on Jul 17, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, but do you want to redirect to the same URL-- meaning that the query string is irrelevant-- or do you want to redirect to a different URL, either by preserving the query string or by putting its information into the path?

<tangent>
I got curious and did a bit of research. ivanvias, you've been asking close variants on the same question since 2009. In that time, questions have been answered by-- among others-- g1smd and jdMorgan. This suggests that your criterion for "expert" is unduly stringent.
</tangent>

penders

8:38 pm on Jul 18, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^mv=[0-4]$
RewriteRule ^([A-Za-z0-9]+)$ [$1.example.com...] [L,R]

But this doesnt work for the [example.com...]

What if you simply remove the QUERY_STRING line from the above ruleset? This should then redirect "https://example.com/firstnamelastname", as well as "https://example.com/firstnamelastname?mv=0", "https://example.com/firstnamelastname?mv=1", etc. (?)

ivanvias

4:46 pm on Jul 19, 2014 (gmt 0)

10+ Year Member



I have these rules on my site:

#################BEGIN RULES ON LIVE SITE
# Externally redirect direct client requests for subdomain-subdirectory URLs which
# resolve to existing subdirectories back to canonical subdomain root URLs
RewriteCond $1 !^(support|about-us|profile|support|features|videos|terms-of-service|about-us|signup|media|includes|modules|cgi-bin|templates|xmlrpc|language|libraries|plugins|administrator|component|images|dev|data)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([a-z0-9\-]+)/[^\ ]*\ HTTP/
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^([a-z0-9\-]+)/(.*)$ [$1.example.com...] [R=301,L]

#
# Externally redirect requests for /features, /about-us, /signup, /images in subdomains to main domain
RewriteCond %{HTTP_HOST} ^([a-z0-9\-]+)\.example\.com
RewriteCond %1 !^www\.
RewriteRule ^(support|about-us|profile|support|features|newserver|videos|about-us|terms-of-service|signup|component|includes|media|cgi-bin|templates|xmlrpc|language|modules|libraries|plugins|administrator|images(/.*))$ [example.com...] [R=301,L]
#
# Externally redirect all www hostnames to non-www hostnames
RewriteCond %{HTTP_HOST} ^(([a-z0-9\-]+\.)*)www\.(([a-z0-9\-]+\.)*)example\.com
RewriteRule ^(.*)$ [%1%3example.com...] [R=301,L]
#


I added these below to redirect these types: [example.com...] etc



RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^mv=[0-4]$
RewriteRule ^([A-Za-z0-9]+)$ [$1.example.com...] [L,R]


However I cannot get these to redirect [example.com...]



If i remove the Query string from the last 3 lines then If i would click on features link on my website It would redirect to [features.example.com...] which is what the rules at the top are preventing.


Can anyone assist with this.

lucy24

7:26 pm on Jul 19, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



However I cannot get these to redirect [example.com...]
Is that the entire requested URL? No trailing slash, no other content?

Also: does "these" mean the rule quoted immediately above, or does it mean all rules? The last listed rule-- the one that's currently missing the "=301" part-- needs to come before the www. redirect.

The whole mv=[0-4] business is a red herring. By default, any redirect will quietly reappend the query string, if any, without changing it. If instead you want all query strings to go away, simply add
?
to the end of the target. There is no need for a separate rule.

ivanvias

9:03 pm on Jul 19, 2014 (gmt 0)

10+ Year Member



Hi,


As is right now the last 3 rules are there to redirect urls that are [example.com...] to 4 Which is working. However I am still getting 404 errors on [example.com...] no trailing slash nor content.

not2easy

9:26 pm on Jul 19, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The last three rules are fine except that they are in the incorrect order to work correctly, and you took off the 301 from the last rule. (which means a temporary 302 is what you would have if it could work.) Try the correction and then see what you have.

lucy24

11:53 pm on Jul 19, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



the last 3 rules are there to redirect urls that are
[example.com...] to 4
Which is working. However I am still getting 404 errors on
[example.com...]
no trailing slash

These are the same redirect and should be the same rule.

Is there some other parameter-- besides mv --that you need to preserve? Otherwise I don't understand why there would be two separate rules.

An URL with no extension and no trailing slash will always lead to a 404 unless you have a rewrite pointing to the place where the content actually lives. Possible exception: if there is a physical file at
example.com/firstnamelastname.xtn
AND MultiViews is active. CMS code also counts as a rewrite. But here we're talking about something that you already know doesn't represent a physical file. Is
example.com/firstnamelastname
supposed to redirect to
firstnamelastname.example.com/
?
What is supposed to happen after that? (There has to be an "after that" because the form
firstnamelastname.example.com
cannot represent a physical location. Servers don't have that many dimensions.)

penders

11:13 am on Jul 20, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^mv=[0-4]$
RewriteRule ^([A-Za-z0-9]+)$ [$1.example.com...] [L,R]

However I cannot get these to redirect [example.com...]

If i remove the Query string from the last 3 lines then If i would click on features link on my website It would redirect to [features.example.com...] which is what the rules at the top are preventing.


Then maybe you need to use the same rule...? For example:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]  
RewriteCond $1 !^features$
RewriteRule ^([A-Za-z0-9]+)$ [$1.example.com...] [L,R]