Wildcard Subdomains using htacess with multiple variables

Please I really need help.
I have written the htaccess code that will create wildcard subdomains using get method for the product page But am actually trying to pass another variable in the get method to make it two variables yet the second variable is not seen.

This is what i want
https://mywebsite.example.com/product.php?id=cloth

Below is the htaccess

RewriteRule ^([aA-zZ])$ product.php?url=$1
RewriteCond %{HTTP_HOST} ^(^.*)\.example.com
RewriteRule (.*) product.php?url=%1

Product page

$id= $_GET['id'];
$url= $_GET['url'];

I don’t see where subdomains come in to this.

Should this not be url=$1?

Also, what are the values of $_GET['id'] and $_GET['url'] in https://mywebsite.example.com/product.php?id=cloth for example, and which values would you expect?

Well, the RewriteCond is matching against a subdomain and passing it as a parameter in the following RewriteRule. Not really what RewriteCond was meant for, but it’s oftentime (ab)used to do this sort of thing anyway.

1 Like

Thank you very much for your reply

The value of $_GET['url'] is the user’s website(subdomain) which is mywebsite from https://**mywebsite**.example.com/product.php?id=cloth.

Then the value of $_GET['id'] is actually the product in the database inserted by the user. it could be cloth as show in the url

https://mywebsite.example.com/product.php?id=**cloth**

However, the htaccess is working very well with the first get variable which is the url but it doesn’t accept any other variable as a get method.

I have been trying to rewrite the rule in a way that it will accept two variables as get method, one used as subdomain and the other used as a get method with a parameter like ?id=cloth

for proper explanation, here is the domain am using, you can access it.
https://mywebsite.efficientpoint.com/test_view?id=cloth

test_view.php

<?php
$id= $_GET['id'];
$sitePostName = $_GET['url'];
?>
<html>
    <body>
        <h1>The website url is (<?php echo $sitePostName; ?>)</h1>
        <h4>The product is (<?php echo $id; ?>)</h4>
    </body>
</html>

.htaccess

RewriteRule ^([aA-zZ])$ test_view.php?url=$1
RewriteCond %{HTTP_HOST} ^(^.*)\.efficientpoint.com
RewriteRule (.*) test_view.php?url=%1

OUTCOME:
The website url is (mywebsite)

The product is ()

Thank you.

Ok let me explain better

You say id, but the .htaccess says url… Can you try this?

RewriteRule ^([aA-zZ])$ test_view.php?id=$1  # replaced ?url with ?id=
RewriteCond %{HTTP_HOST} ^(^.*)\.efficientpoint.com
RewriteRule (.*) test_view.php?url=%1

Noooo. I don’t want to replace url with id. I want to get the value of Id on the page using get method. While the url function will still remain as subdomain.

The function of Url is to derive subdomain while id is to fetch a particular product from the database being inserted by the user with url.

for e.g
We have a user in the database with a name = john and slug_url = mywebsite
John inserts products into a table called product_table.
while inserting, his slug_url is saved in the database.

Now when john navigate to https://mywebsite.example.com/test_view?id=5
We should be able to select * from product_table where slug_url = ‘$url’ AND id = ‘$id’

The problem I have is that I don’t really know how to rewrite htaccess to accept other get variables apart from url. It’s only url that is being defined.

Can you please try my code and see what it does before you claim it won’t work?

I have tried your code sir and its working but i can’t still get the value of id ($_GET[‘id’]).

Your code is what am using right now.
http://gold.efficientpoint.com/test_view?id=2

since the value of id is 2
then on the page, you will see The product is ()

2 should be in the bracket. i.e. <h2>The product is (<?php echo $_GET['id']; ?>).

Alright. How about we combine them? Does this work?

RewriteCond %{HTTP_HOST} ^(^.*)\.efficientpoint.com
RewriteRule ^([aA-zZ])$ test_view.php?url=%1&id=$1

I assume, if you make redirect on path?query your old query-part will replaced with new.

@igor_g I was assuming that’s happening too, even though Apache should not normally operate that way.

Which is why I decided to sidestep the issue and combine the two rules into one :smiley:

I have used it and am using it right now. Its still not recognizing the variable id.

Can you post your full .htaccess code please?

Ok sir

RewriteRule ^([aA-zZ])$ test_view.php?url=%1&id=$1
RewriteCond %{HTTP_HOST} ^(^.*)\.efficientpoint.com
RewriteRule (.*) test_view.php?url=%1

No wonder it doesn’t work …

Can you please replace all of that with the following?

Options -MultiViews -Indexes

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(^.*)\.efficientpoint.com
RewriteRule ^([aA-zZ])$ test_view.php?url=%1&id=$1

Wow!
It almost worked. This time around variable id is defined but variable url is undefined.

You can check out the page
http://gold.efficientpoint.com/test_view.php?id=2

I guess we almost got it.

Thank you.

Sir i have actually seen something similar but i don’t really understand it. Maybe you can check it as well.

The last reply seems to show success.

Looking through this thread, I kept thinking that the %1 value is constantly being reevaluated BECAUSE mod_rewrite does not stop until there are no more matches.

The code above (from rpkamp) matches a subdomain (IF IT EXISTS) before the dot separator for the domain but requires no dot character in the {REQUEST_URI} to provide a redirection to {subdomain}.efficienthost.com/test_view.php?url={subdomain}&id={REQUEST_URI}.

I’m not sure why rp included the Options (other than good practice) nor the double ^ for the subdomain. Also, I vastly prefer designating the allowed characters in the subdomain string, including the dot separator. Making the subdomain required via the RewriteCond is correct (but the * makes all but the dot separator optional).

Question. Are you trying to rewrite to efficienthost.com? With or without www? with gold?

In other words, please give an example of the submitted URL (with subdomain) and the desired URL (after redirection) for a better answer.

Regards,

DK