Forum Moderators: phranque

Message Too Old, No Replies

Rewrite Query String as Subdirectory and Display as Subdomain

         

ItsDavid

7:19 pm on Feb 19, 2016 (gmt 0)

10+ Year Member



I have search every possible resource however, I have not been able to find the help i need so i thought i would ask here.

I am not highly educated in .htaccess rewrites so please bare with me if i might use the wrong term in the wrong place or not explain things as clear.

I am trying to rewrite some query urls as a subdomain and my understanding is that i have to first rewrite the query internally as a subdirectory which i have done for one query string in the series of query strings.

I currently have wildcard domains active on my website and i also have rewrite code in place to remove (WWW) from all urls.

RewriteRule ^profile$ /index.php?a=4 [L]

I am now trying to rewrite/redirect (which ever is the correct term) that to a subdomain like

profile.mysite.com

Now i currently have this code below in place that actually redirects to my desired subdomain in the browser.

Redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index.php)\?a=4\ HTTP/
RewriteRule ^(index.php)$ profile.mysite.com? [R=301,L]


Rewrite
RewriteCond %{HTTP_HOST} ^profile.mysite.com$
RewriteRule ^$ profile.mysite.com/index.php?a=4 [L]


The problem i am now facing is that when ever i go to the subdomain url i am getting a message in the browser saying

"The page isn't redirecting properly" however the URL in the browser displays >>>> profile.mysite.com

Can anyone advise me what the problem might be that is causing it to no redirect properly?

My second question to this is that when i user is in their profile there are many other query links for different account management pages with a query url structure listed below.

mysite.com/index.php?a=4 >>>> I would like to display as > profile.mysite.com

mysite.com/index.php?a=4&b=1 >>>> I would like to display as > profile.mysite.com/active

mysite.com/index.php?a=4&b=2 >>>> I would like to display as > profile.mysite.com/expired

mysite.com/index.php?a=4&b=8 >>>> I would like to display as > profile.mysite.com/messages


This pattern of a=(A Number)&b=(A Number) goes on and on throughout the website so my question here is there a simple way to rewrite these urls or do i need to rewrite each one of them one-by-one?

Thank you in advance for any help that can be provided.

lucy24

8:35 pm on Feb 19, 2016 (gmt 0)

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



RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index.php)\?a=4\ HTTP/
RewriteRule ^(index.php)$ profile.mysite.com? [R=301,L]

Why is the element "index.php" (should be index\.php but this is a non-lethal error) in parentheses although nothing is to be captured? I strongly suspect there's a missing ? so both places should say
(index\.php)?
meaning "the actual words 'index.php' are optional; the important part is redirecting the query string". Yes, that means the middle of the condition will look like this
(index\.php)?\?
which looks goofy but is perfectly correct.

RewriteRule ^(index.php)$ profile.mysite.com? [R=301,L]

The target of an external redirect (you're using the two terms correctly in the examples) always needs a full protocol-plus-domain. Otherwise you're redirecting to some-complicated-filepath/blahblah/etcetera. This is especially important if the redirect target involves a different (sub)domain.

mysite.com/index.php?a=4 >>>> I would like to display as > profile.mysite.com
mysite.com/index.php?a=4&b=1 >>>> I would like to display as > profile.mysite.com/active
mysite.com/index.php?a=4&b=2 >>>> I would like to display as > profile.mysite.com/expired
mysite.com/index.php?a=4&b=8 >>>> I would like to display as > profile.mysite.com/messages

This pattern of a=(A Number)&b=(A Number) goes on and on throughout the website so my question here is there a simple way to rewrite these urls or do i need to rewrite each one of them one-by-one?

Oops, here you really do mean "redirect". And then there will be an internal rewrite to serve content from index.php?blahblah.

How many possible values are there for "b"? Is there any possibility that they'll come in backward, like "b=2&a=4"? Even if humans don't do it, you can't put anything past search engines. Here you're looking at a RewriteCond involving %{QUERY_STRING}.

If there are only a few possible b's, it is easiest to write each rule separately. If there are dozens of possible values, it's better to detour via a php script that does the lookup for you. (If it were your own server you could also use a RewriteMap, but this is htaccess, right?)

And assuming this is htaccess: are these rules located in an htaccess file that will be seen by all requests, regardless of subdomain?

ItsDavid

4:21 am on Feb 20, 2016 (gmt 0)

10+ Year Member



Lucy24, I made the edits you mentioned above to the index.php and now my url now looks like [profile.mysite.com...] instead of profile.mysite.com

As for possible "b" values there may be a dozen or more but none of them come in backwards such as "b=2&a=4" the urls scale upwards in alphabet and numbers such as

index.php?a=11&b=$4&c=65&d=2

However, as you mention the ones i am focused on i could just create a rewrite/redirect rule for each one-by-one. I am not sure if having that many rules in the .htaccess file would cause me any performance problems with my server and site speed.

That is correct this is .htaccess

Yes the .htaccess will be seen by all regardless of subdomain because all of my subdomains used for this site/project are wildcard.

Thank you kindly for your response.

ItsDavid

1:38 pm on Feb 20, 2016 (gmt 0)

10+ Year Member



I did forget to add to my first reply that i am getting the message in my browser

Not Found

The requested URL /(index.php) was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

The URL displaying in my browser is profile.mysite.com/%28index.php%29?%3fa=4

lucy24

7:55 pm on Feb 20, 2016 (gmt 0)

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



The requested URL /(index.php)

You mean, with parentheses, as reflected in %28 etcetera?! If so, something is seriously wrong. In fact I can't even figure out where that would be coming from, unless you did some over-hasty cut-and-paste and ended up with (index.php) in the target. Let's see your RewriteRules again, as they exist right this instant.

ItsDavid

12:23 am on Feb 21, 2016 (gmt 0)

10+ Year Member



Here are my rules exactly top to bottom. I have been trying to narrow down the issue however, knowledge of .htaccess is very limited.

At this point with the rules posted below clicking on a link mysite.com/index.php?a=4 forwards me to the following URL in my browser profile.mysite.com/index.php?a=4 and that is exactly how it displays in the browser.

I get a message "The Page is Not Redirecting Properly"


Options +FollowSymlinks
RewriteEngine On
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]


RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://%{HTTP_HOST}/ [R=301,NS,L]


RewriteRule ^/?(profile)/$ /index.php?a=4 [L]


#Rewrite
RewriteCond %{HTTP_HOST} ^profile\.mysite\.com
RewriteRule ^$ http://profile.mysite\.com/index\.php\?a=4 [L]


# Redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?a=4\ HTTP/
RewriteRule ^(index\.php)$ http://profile.mysite.com? [R=301,L]

[edited by: phranque at 10:50 am (utc) on Feb 21, 2016]
[edit reason] added code tags for readability [/edit]

lucy24

2:30 am on Feb 21, 2016 (gmt 0)

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



:: hang on, I'm asking a moderator to make the post readable ::

Meanwhile: Is this htaccess file going to be seen by more than one domain, and if so, will some of the external redirects apply to more than one domain?

ItsDavid

3:03 am on Feb 21, 2016 (gmt 0)

10+ Year Member



I don't believe it will be.... I say that because I currently use wildcard domains for regions on the site that are added via the control panel such as ny.mysite.com however, that coding is all done in the php script I am using to run the site. On the other hand it might be seen by these wildcard domains once a person selects/sets their region and is brought to the URL for their region such as ny.mysite.com because their selection is remembered while browsing the site. Now if they select the my profile link we are discussing here /index.php?a=4 they are brought to their account management page (URL) which is the URL that displays their account management page with no ny.mysite.com displayed it is displayed as mysite.com/index.php?a=4

Sorry for my lack of understanding but what I think you mean is will the external redirects apply to more then mysite.com (such as mysite.com, mysecondsite.com, mythirdsite.com etc...) if that is what you mean then the answer is no. It will only apply to mysite.com

Thank you again for all the help.

ItsDavid

12:27 am on Feb 23, 2016 (gmt 0)

10+ Year Member



Lucy24, Just wanted to follow up to see if you were able to make sense of what is going on?

lucy24

4:11 am on Feb 23, 2016 (gmt 0)

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



Oops, I only just saw that phranque fixed the post
:: pause to wave arms happily because he's been gone soooo long ::
so I'll give it a fresh look tomorrow when I'm awake, keyboard permitting.

ItsDavid

4:18 am on Feb 23, 2016 (gmt 0)

10+ Year Member



Okay, Thank You. I am still pulling my hair out with it lol.

lucy24

8:15 pm on Feb 23, 2016 (gmt 0)

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



What follows is a picking-apart of the RewriteRules quoted above. But there are some unanswered questions, of which the most important is:
How many subdomains are on this site, and where are they located? On your server, are the subdomains located inside the directory that contains the files for the primary domain? Or are they side by side? (Option C is that they live on an entirely different server. This is perfectly possible, but doesn't seem to be your situation.)

The part we need to clarify is whether requests for any subdomain will be seen by the htaccess file for the primary domain. If yes, everything gets more complicated.

In wht follows (and, er, precedes), any missing a's are due to my keybord misbehaving. So do not copy-and-paste anything! :) It also doesn't seem to like * asterisks and 7 (the numeral seven).

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
The first and third items aren't strictly necessary, though they will do no harm.

+FollowSymLinks * is required if mod_rewrite is to be used in htaccess. So on shared hosting, the host would be nuts not to turn it on in the config file, just to avoid all those technical-support questions about "Why aren't my RewriteRules working?"

The RewriteBase is only used when the target of a rewrite doesn't start in a / or http. It's safer to use the / yourself. Besides, it saves a few bytes unless you have a huge number of internal rewrites.

* I double-checked this recently. Apache directives aren't case-sensitive, although arguments usually are.

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
whitespace? You out there? I'm not sure you can do this in mod_rewrite; doesn't {ENV:blahblah} mean an environmental variable that you yourself have set?

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]

The [NC] flag in the rule is superfluous, since nothing in the pattern specifies a case. In the Condition it's outright wrong, since part of your domain-name-canonalization is about getting everyone to use exactly the same form of the name.

The element (.*) in the condition really, really makes no sense, because you're not going to get requests for "www./directory" like that.

You said earlier, didn't you, that this specific htaccess is only seen by one domain? If so, the domain-name-canonicalization redirect looks like this
RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]
and it will be your last external redirect among your RewriteRules. (The "index" redirect, which we're about to get to, will generally be the second-to-last.)

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://%{HTTP_HOST}/ [R=301,NS,L]
Again, HTTP_HOST doesn't belong here. Use the actual domain name.

Note, too, that search engines will ask for "index.html" even if you've never used .html. So you may want to replace \.php in both places with "index\.(php|html)" or even "index\.\w" without closing anchor.

Are there no deeper directories? If there are, they can all be handled in a single RewriteRule. We can get back to that. This applies both to real, physical directories and to pseudo-directories in URLs, like /profile/ below. Remember that search engines will ask for nonexistent URLs. They may end up with a 404, which is fine because they brought it on themselves, but in situations like "index.html" a redirect is cleaner.

RewriteRule ^/?(profile)/$ /index.php?a=4 [L]
You don't need to capture "profile" since you're not reusing it. And in htaccess-- or, for that matter, any directory section-- you don't use the leading / so just omit it.

#Rewrite
RewriteCond %{HTTP_HOST} ^profile\.mysite\.com
RewriteRule ^$ http://profile.mysite\.com/index\.php\?a=4 [L]
Oh, wait, so you do have more than one hostname on the same htaccess. Subdomains count as different hosts.

Far more important: The moment you use a full protocol-plus-domain in a target, the rule changes into an external redirect. If you don't use the [R=301] flag, it defaults to 302.

# Redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?a=4\ HTTP/
RewriteRule ^(index\.php)$ http://profile.mysite.com? [R=301,L]
This rule is in the wrong place. All external redirects need to go before all internal rewrites. Besides, didn't you say this earlier?

In your original post you said
I currently have wildcard domains active
Did you mean subdomains? How many of them are there? "wildcard" is an option your host gives you, but it doesn't always mean that you really have potential infinite subdomains like
user1.example.com/
user2.example.com/
user3.example.com/
:: pause here for fond memories of longago virtualave site ::

If you're only using specific, named subdomains such as "profile", any RewriteRules should be written to take those into account. This, in turn, means that any earlier RewriteRules that apply only to the primary domain will need a RewriteCond looking something like this
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
Note that this is the only situation where "www." in a condition is optional, because your domain-name-canonicalization comes later.

whitespace

10:16 pm on Feb 24, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



(Sorry, not read through the rest of the thread just yet...)


RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

whitespace? You out there?


This in itself would seem to be OK. REDIRECT_STATUS being an internal Apache ("environment"?) variable (it would seem that Apache lumps these together with userland environment variables). (This is how they appear to PHP as well.) It will be set to 200 on the first successful internal rewrite (subrequest?). So this is intended to prevent a rewrite loop. However, other subrequests can also set this... mod_dir, ErrorDocument, etc. So it will also hold the HTTP status (403, 404, etc.) of the last error state.

whitespace

2:37 pm on Feb 25, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



mysite.com/index.php?a=4&b=8 >>>> I would like to display as > profile.mysite.com/messages

This pattern of a=(A Number)&b=(A Number) goes on and on throughout the website so my question here is there a simple way to rewrite these urls or do i need to rewrite each one of them one-by-one?


Have you changed the actual URLs in your application to look like "profile.mysite.com/messages", or are you trying to do everything in .htaccess?

Unless the "profile.mysite.com" is physically hosted at the same place as "mysite.com" (I assume it must be?) then you're going to have problems trying to internally rewrite back to the real URL. You'll need to rewrite back to "profile.mysite.com/index.php?a=4&b=8" or you'll have to dip into mod_proxy (I think)?

You said earlier, didn't you, that this specific htaccess is only seen by one domain?


I'm kinda wondering how these wildcard (sub)domains are working if this .htaccess file is only seen by one domain/host?




RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]



The [NC] flag ....... In the Condition it's outright wrong, since part of your domain-name-canonalization is about getting everyone to use exactly the same form of the name.

The element (.*) in the condition really, really makes no sense, because you're not going to get requests for "www./directory" like that.


As I understand it, the NC flag in the condition is required just to catch malformed requests (browsers will normalize/lowercase the host). It's checking for equality (www, wWw or WWW, etc), not inequality (which would indeed be wrong to use the NC flag).

The element (.*) in the condition is required to capture the host less the www subdomain, and the backreference (%1) in the host part of the substitution.

ItsDavid

7:12 pm on Feb 25, 2016 (gmt 0)

10+ Year Member



I am going to attempt to answer this as best as i can in hopes i do it in an understandable way.

Lucy24, Yes you are correct i did mean sub-domains as for how many there are, I don't have an exact count but i know it is a couple of hundred. I have one for each State in the US and varying amounts of sub-regions under each main region.

Main Region >>>>> New York
Sub Region >>>>> Westchester
Sub Region >>>>> Hudson Valley

ETC...

The wildcard sub-domains are not controlled by .htaccess I only use .htaccess to remove (WWW) from the main domain and all sub-domains. I have *.mysite.com added in my cPanel and in my website administration panel i have a place to add a region such as NY and this creates a sub-domain ny.mysite.com this is all done with PHP.

Now if someone has their region set to ny.mysite.com and wants to navigate to their profile page URL which is mysite.com/index.php?a=4 they are now taken to the main domain URL (Profile URL). How the sub-domians are being generated within the PHP i couldn't explain.

Hope this explains it clearer.

lucy24

7:12 pm on Feb 25, 2016 (gmt 0)

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



It's checking for equality (www, wWw or WWW, etc), not inequality (which would indeed be wrong to use the NC flag).
Oops, you're right, my brain went on autopilot and I was thinking of domain-name canonicalization. This one's a positive match, not the negative I was thinking.

The element (.*) in the condition

Here I only meant it should be .+ rather than .* since a null capture can't occur. In practice, of course, it will make no difference, but I like to keep a distinction ("definitely something here" vs. "maybe something here").

David, still with us? Let's hear more about (1) the subdomains that actually exist on your site and (2) their physical directory structure. Did you at some point specify whether this is your own server or shared hosting? I'm reasonably sure you can't do proxy stuff outside of the config file (for example, serving content from subdomain.example.com when the request is for plain example.com).

whitespace

8:33 pm on Feb 25, 2016 (gmt 0)

10+ Year Member Top Contributors Of The Month



(It all goes quiet and then suddenly... a simulpost!)

Here I only meant it should be .+ rather than .* since a null capture can't occur.


Ah, yes. :)

ItsDavid

2:55 am on Feb 26, 2016 (gmt 0)

10+ Year Member



Lucy24, I am hanging on for dear life trying to Catch-Up to the crowd lol.

Please excuse my limited understanding here.

Let's hear more about (1) the subdomains that actually exist


What information do i need to provide? I don't understand because the only thing i know is that i have Wildcard Sub-Domains active on my current server which is a VPS. The method used to create these sub-domains is as follows.

In my administration panel i have a module that allows me to add or delete regions. I can choose (with a selections option) to use sub-domains as sub-domains or display them (in the URL) as a query string. Example below:

mysite.com/index.php?region=1025

Now as my site is currently configured that exact URL would display as

ny.mysite.com if i have the option to use regions as sub-domains selected.

As i add more and more regions each one is assigned its own region number automatically.

These regions are used to display any content that might be on mysite within that region and that applies regardless if i use the sub-domain or query URL option.

These options or settings have no effect on my .htaccess file meaning nothing (RULES) is added or deleted to the file based on my website settings. So everything must be controlled with PHP only.

Another note to add is that each region i add to my website using the admin panel gets added into my database and when someone selects the region they are from their selection is set and remembered using cookies.

Now after a region is selected and set with a cookie it makes no difference what region they have set when they go to their "Profile" page which is the URL in question here

mysite.com/index.php?a=4

If i region was selected it does not effect this url.

All i am trying to do is make it so that instead of the user seeing mysite.com/index.php?a=4 in the address bar it will actually look like they are at profile.mysite.com when they are on their account profile homepage however, the actual content is coming from mysite.com/index.php?a=4 without them seeing that in the address bar.

Another thing is that if i should miss changing all URL's in the source from mysite.com/index.php?a=4 to profile.mysite.com and they click a link or a search engine finds the default link of

mysite.com/index.php?a=4 it will automatically redirect the user to the new profile.mysite.com

I really hope i am explaining things in an understandable way. I am horrible at explaining things.

I am not clear as to weather i have Proxy stuff or not but with that said i don't have any problems with the regions when used as sub-domains or displaying any of the content within them. Not sure if this would be an indication or not.

ItsDavid

3:22 am on Feb 26, 2016 (gmt 0)

10+ Year Member



I made an attempt to change this

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]


To this

RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]


and the results is that it only removes (WWW) from the main domain mysite.com however, if i go to s region sub-domain ny.mysite.com i am getting a message in my browser that the page is not redirecting properly.

With that said i am going to say that each Wildcard sub-domain sees my .htaccess file. I do not have any of the region sub-domains manually added to my cPanel. They all pull the content from the main domain mysite.com


Use of this code was the only way i was able to remove index.php from the main homepage URL as well as the sub-domain URL's such as

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://%{HTTP_HOST}/ [R=301,NS,L]


mysite.com/index.php

and

ny.mysite.com/index.php

[edited by: ItsDavid at 3:46 am (utc) on Feb 26, 2016]

lucy24

3:30 am on Feb 26, 2016 (gmt 0)

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



... and just to make it more interesting, there are two vaguely similar threads going on at the same time, "Didn't we answer that already? Oops, no, different topic." And I only this moment realized that you and I posted at the identical minute earlier today, so I was asking questions you thought you'd already answered and no wonder you're crying "What more does she want?!" :(

I don't have an exact count but i know it is a couple of hundred

Oh, yikes, so it's not just a case of example.com vs. profile.example.com and that's all. OK then...

if someone has their region set to ny.example.com and wants to navigate to their profile page URL which is example.com/index.php?a=4 they are now taken to the main domain URL (Profile URL). How the sub-domains are being generated within the PHP i couldn't explain.
:: glancing upward in hopes that whitespace, who speaks php, has answered this already ::

In the user’s address bar, are the profiles supposed to look like this
example.com/profile
or like this
profile.example.com/
or like this
ny.example.com/profile
ca.example.com/profile
? (Where did I get /profile from? Please say it wasn't that other thread.)

Where are the directories for your subdomains? When you FTP into your site (or whatever you do to get at the files) does it look like this
[] example.com
[]sub1
[]sub2
[]sub3
all on the same level, or are the subdomain directories inside the directory for example.com ... or do the subdomain directories not exist at all? (That is, there are no physical files for ca.example.com, mt.example.com, ny.example.com and so on; it's all handled in index.php.) If the subdomains exist only as URLs, not as physical directories, that may actually make things a lot easier.

All i am trying to do is make it so that instead of the user seeing example.com/index.php?a=4 in the address bar it will actually look like they are at profile.example.com when they are on their account profile homepage however, the actual content is coming from example.com/index.php?a=4 without them seeing that in the address bar.

Another thing is that if i should miss changing all URL's in the source from example.com/index.php?a=4 to profile.example.com and they click a link or a search engine finds the default link of

example.com/index.php?a=4 it will automatically redirect the user to the new profile.example.com
It begins to be less muddy. What you're doing is the redirect-to-rewrite two-step, where you're changing old ugly URLs to new pretty URLs, while continuing to serve content from the old ugly location. Have I got that right? Things like /index.php?a=1&b=2 used to exist as publicly visible URLs, and now you want to get rid of them?

The simplified version goes like this:
RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} some-exact-stuff-here
RewriteRule index\.php http://www.example.com/profile/some-other-stuff-here [R=301,L]
That's the external redirect; it comes first. The line about THE_REQUEST means "The user explicitly asked for an URL with a query string".

And then this:
RewriteRule ^profile/some-stuff-here /example.com/index.php?buncha-stuff-here [L]
and that's where I need to know if it's profile.example.com, or ny.example.com/profile, or ... uh ... whatever the heck it is.

Now, assuming that all requests pass through the same htaccess-- meaning that either the subdomains are inside the main site's directory, or the subdomains don't physically exist at all-- you may also need a RewriteCond looking at %{HTTP_HOST}. But not until I'm sure what the host is.

Final edit: I don't have any personal experience with cPanel. (My host is one of the big ones, so they've got their own proprietary control panel.) I think whitespace does, though, so if we wildly contradict each other, he's right and I'm wrong. There's something weirdly satisfying about being able to say that without having to look anyone in the eye.

ItsDavid

3:55 am on Feb 26, 2016 (gmt 0)

10+ Year Member



For starters you got "profile" from this thread LOL.

In the user’s address bar, are the profiles supposed to look like this


they should look like profile.mysite.com

Where are the directories for your subdomains? When you FTP into your site (or whatever you do to get at the files) does it look like this


The sub-domains do not exists. they are all handled dynamically with index.php

The sub-domains only exists as URL's


It begins to be less muddy. What you're doing is the redirect-to-rewrite two-step, where you're changing old ugly URLs to new pretty URLs, while continuing to serve content from the old ugly location. Have I got that right? Things like /index.php?a=1&b=2 used to exist as publicly visible URLs, and now you want to get rid of them?


You are 100% correct

ItsDavid

11:09 pm on Feb 27, 2016 (gmt 0)

10+ Year Member



Any insight on how to get this mysite.com/index.php?a=4 to rewrite and display as profile.mysite.com

lucy24

1:43 am on Feb 28, 2016 (gmt 0)

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



That's where the two-step comes in. First redirect:
RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} \ba=4\b
RewriteRule ^(index\.php)?$ http://profile.example.com/ [R=301,L]
meaning "If they ask for something with suchandsuch query string, redirect to the without-query URL". It's a little tricky because there are really two potential changes: the hostname, and the query string. After giving it some thought I decided you don't need to mention %{HTTP_HOST} at all, because no matter what you request, the point is there's not supposed to be a visible query string. I said "\ba=4\b" on the off chance that you've got an "a=40" or possibly "a=467835" leading to some other page, and similarly "idea=4" or "panorama=4" and so on. (The fancy form uses
(^|&)a=4($|&)
instead of \b for the query-string anchors, but in most situations you don't need to get that precise.)

This rule goes near the beginning of the mod_rewrite section of your htaccess: after the access-control RewriteRules if you've got any, but before the more generic redirects.

Then the rewrite, coming near the end of your htaccess
RewriteCond %{HTTP_HOST} profile\.example\.com
RewriteRule ^$ /index\.php?a=4 [L]
I may have left something out, but that's the idea. If you have a cluster of similar rewrites, all the rules will go in the same place. On paper you're serving content from "profile.example.com" rather than "example.com" but your php will figure out that part.

Which reminds me:
There are two reasons for using the exact name "example.com" (or example.org or example.xyz). One is to avoid naming your own site. The other is to prevent auto-linking in code-oriented subforums such as this one. The Forums do this with all hostnames other than example dot any-tld, which is why locutions like
http://%1/$1
http://subdomain.example.com/
have to be enclosed in [ code ] tags. Otherwise you get
http://%1/$1
[subdomain.example.com...]
(moderators don't change! I did it on purpose ;) ) which makes it just a little difficult to see what you're asking.

ItsDavid

4:06 am on Mar 1, 2016 (gmt 0)

10+ Year Member



Okay, so i have still been trying to get his this thing to work and the closest i can get is.

This


RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} \ba=4\b
RewriteRule ^(index\.php)?$ http://profile.example.com/ [R=301,L]


and this


RewriteCond %{HTTP_HOST} profile\.example\.com
RewriteRule ^$ /index\.php?a=4 [L]


Displays

http://profile.mysite.com/?a=4


With a message in the browser

The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

This problem can sometimes be caused by disabling or refusing to accept cookies.

lucy24

5:18 am on Mar 1, 2016 (gmt 0)

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



An error message from your browser means that the site is redirecting over and over again. If the problem were with the internal rewrite, the only error would show up in your own site's error logs. So we have to figure out what's causing it to keep redirecting...

Oh, ###, oh, ###. I am so sorry. The target should look like this:

http://profile.example.com/?

See the question mark? That means get rid of the query string. Otherwise the redirect will keep looping back on itself. So, with the addition of one little character:

RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} \ba=4\b
RewriteRule ^(index\.php)?$ http://profile.example.com/? [R=301,L]
In Apache 2.4 there is also the flag [QSD] for "query string discard", but this is 2.2, right? In any case, may as well stick with one byte (the question mark) instead of four (comma plus three letters).

:: noting with interest that Apache has now put up the "legacy release" warning on docs for 2.2, which I think means 2.6 is just around the corner ::

ItsDavid

6:43 pm on Mar 1, 2016 (gmt 0)

10+ Year Member



Lucy24,

I have tried adding ? as you suggested however after adding "?" when i click



http://mysite.com/index.php?a=4



It just reloads the home page
 http://mysite.com


Here is my code in the exact order i have it in my .htaccess file.



RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} \ba=4\b
RewriteRule ^(index\.php)?$ http://profile.mysite.com/? [R=301,L]

RewriteCond %{HTTP_HOST} profile\.mysite\.com
RewriteRule ^$ /index\.php?a=4 [L]


I don't know what version of Apache my host is running on my server. Do you have any advise how to check and find out?

lucy24

10:11 pm on Mar 1, 2016 (gmt 0)

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



I don't know what version of Apache my host is running on my server.

They may or may not tell you. (Mine cites "security reasons" -- and then turns right around and gives you the code for a mod-checking function that in effect tells you what version you're on, since mod names change with each release). It shouldn't make any difference here, though. The trailing ? is still valid.

It just reloads the home page

Check with an extension such as LiveHeaders in Firefox to see whether the redirect-- one or more of them, but evidently not thirty-- is taking place at all. You can also look at your site logs to see how many separate requests get received. We now need to figure out whether it's an Apache problem or a php problem.

:: looking vaguely around for whitespace, who speaks fluent php ::

Where in your htaccess is the rule for this redirect located? Again, it should be near the beginning of your external redirects and definitely before all the stuff involving internal rewrites.

Come to think of it: how are your logs configured? Is it a single big log file that includes information for all the subdomains, and if so, does each log entry specify a hostname?

ItsDavid

11:49 pm on Mar 1, 2016 (gmt 0)

10+ Year Member



So i have contacted my host and they have advised me that i am running Apache Version 2.2.31

Here are the HTTPLive Headers responses i am getting.

This response is from the Generators Tab



#request# GET http://mysite.com/index.php?a=4
GET /index.php?a=4
#request# GET http://profile.mysite.com/
#redirect# GET /
#request# GET http://mysite.com/index.php
#redirect# GET /index.php
#request# GET http://mysite.com/
#redirect# GET /



This is the response from the Headers Tab




http://mysite.com/index.php?a=4

GET /index.php?a=4 HTTP/1.1
Host: mysite.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:44.0) Gecko/20100101 Firefox/44.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://mysite.com/
Cookie: XXXXXX
Connection: keep-alive

HTTP/1.1 301 Moved Permanently
Date: Wed, 02 Mar 2016 00:24:50 GMT
Server: Apache
Location: http://my.mysite.com/
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 191
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
http://my.mysite.com/

GET / HTTP/1.1
Host: profile.mysite.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:44.0) Gecko/20100101 Firefox/44.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://mysite.com/
Cookie: XXXXXXX
Connection: keep-alive

HTTP/1.1 302 Moved Temporarily
Date: Wed, 02 Mar 2016 00:24:50 GMT
Server: Apache
X-Powered-By: PHP/5.4.45
Cache-Control: no-cache, must-revalidate
Expires: Sat, 26 Jul 1997 05:00:00 GMT
Location: http://mysite.com/index.php
Vary: User-Agent,Accept-Encoding
Content-Encoding: gzip
Content-Length: 20
Connection: close
Content-Type: text/html
----------------------------------------------------------
http://mysite.com/index.php

GET /index.php HTTP/1.1
Host: mysite.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:44.0) Gecko/20100101 Firefox/44.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://mysite.com/
Cookie: XXXXXXX
Connection: keep-alive

HTTP/1.1 301 Moved Permanently
Date: Wed, 02 Mar 2016 00:24:50 GMT
Server: Apache
Location: http://mysite.com/
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 189
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------
http://mysite.com/

GET / HTTP/1.1
Host: mysite.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:44.0) Gecko/20100101 Firefox/44.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://mysite.com/
Cookie: XXXXXXX
Connection: keep-alive

HTTP/1.1 200 OK
Date: Wed, 02 Mar 2016 00:24:50 GMT
Server: Apache
X-Powered-By: PHP/5.4.45
Cache-Control: no-cache, must-revalidate
Expires: Sat, 26 Jul 1997 05:00:00 GMT
Set-Cookie: region=0; expires=Wed, 20-Jul-2016 00:24:50 GMT; path=/; domain=.mysite.com
Accept-Ranges: none
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Length: 8944
Connection: close
Content-Type: text/html
----------------------------------------------------------

ItsDavid

8:44 pm on Mar 3, 2016 (gmt 0)

10+ Year Member



Was the information i provided above helpful in figuring out what the problem might be here?

I have not clue where to start to find out whats going wrong.

lucy24

3:03 am on Mar 4, 2016 (gmt 0)

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



Well, the good news is that I don't believe this is an Apache problem.

The bad news is that I don't believe this is an Apache problem ... which means it has to be a php problem.

Yo! whitespace! Don't think I didn't see you trying to sneak out the door.

http://example.com/index.php?a=4

GET /index.php?a=4 HTTP/1.1
Host: example.com

HTTP/1.1 301 Moved Permanently
Location: http://my.example.com/
So far so good: The intended redirect took place. (I'm assuming your fingers typed "my" when they meant to type "profile", since otherwise the rest makes no sense.) The browser will now make a fresh request:

GET / HTTP/1.1
Host: profile.example.com

HTTP/1.1 302 Moved Temporarily
Location: http://example.com/index.php
wtf? This should absolutely NOT be happening. I smell an internal rewrite that is somehow getting sent out again as an external redirect, causing the browser to make a request that it should never have made.

GET /index.php HTTP/1.1
Host: example.com

HTTP/1.1 301 Moved Permanently
Location: http://example.com/

GET / HTTP/1.1
Host: example.com

HTTP/1.1 200 OK
This last bit is OK: an explicit request for /index.php has been redirected to / alone. That leaves us only with
HTTP/1.1 302 Moved Temporarily
Location: http://example.com/index.php

and you're going to need to figure out where it's coming from. The only way it can be an Apache problem is if a rule that is supposed to create an internal rewrite (target beginning in / with [L] flag alone) is instead sending back an external redirect (target beginning in full protocol-plus-domain, possibly with [R] flag but definitely not [R=301]). If you can't find anything of the sort in your htaccess, it's time for a side trip to the php subforum, where you will meet some familiar faces although not mine.