How to get subdomain and parameters in .htaccess

My link is like this:
http://subdomain.mysite.com/123

With this code in .htaccess I can get subdomain But I do not know how to get ‘123’


RewriteEngine on

RewriteCond %{HTTP_HOST} ^([^.]+)\\.mysite\\.com$ [NC]

RewriteCond %1 !^(www|ftp|mail)$ [NC]

RewriteRule ^.*$ index.php?sub=%1

mo,

What you need is to learn some regex (see the tutorial Article linked in my signature if you need a place to start).

Q1. WHY are you concerned with links to ftp and mail subdomains?

Q2. WHAT do you want to do with 123?

The answer to your question, though, is that 123 is already available as an Apache variable: {REQUEST_URI}.

Regards,

DK

David,

Are you sure about that?

Try


RewriteEngine On
RewriteRule .? http://www.google.com/?q=%{REQUEST_URI} [L,R=302]

and open up /123 on that domain with some text, see what google’s searching for: not “123”, but “/123” because %{REQUEST_URI} contains the leading slash (I’m fairly certain %{REQUEST_URI} contains the URI specified after GET in the HTTP request, even though no manual confirms this – but then as we both know the Apache manuals tend to be pretty fague).

If you don’t want that leading slash, you’d have to use


RewriteEngine On
RewriteRule ^(.*)$ http://www.google.com/?1=$1 [L,R=302]

I haven’t found a way to make this happen that doesn’t use that dreaded evil .*

Rémon,

Yes. [noparse]http://domain/123[/noparse] What else is 123 supposed to be? It’s certainly not a query string (no ? to denote the use of a query string). Further, you’ve suggested the use of the :kaioken: EVERYTHING :kaioken: atom which is acceptable as you’ve used it (redirecting to another domain) but will be loopy on one’s own website.

mo,

If your variable is merely a series of digits, replace ^.*$ with ^([\d]+)$ or ^([0-9]+)$ (same thing, different way to specify digits. However, I am still wondering what you’re trying to do

Regards,

DK

David, I’m merely pointing out that %{REQUEST_URI} contains a leading slash (/123), since you seem to be thinking it doesn’t (i.e. you seem to be thinking it just contains “123”).
We’ve had this discussion before …

Rémon,

I know. I’m the one who insists that the / magically appears. However, since 123 is not in a query string, it’s still a far better answer and should be tried before being discarded for something obviously incorrect. :wink:

Regards,

DK

Yes, totally agree on that one :slight_smile: