Dynamic subdomain with .htaccess and php - php

I have a page like
index.php?username=thurein
i need to get with subdomain like
thurein.mydomain.com
how could i get it?
I have done like ..
RewriteEngine on
RewriteCond %{HTTP_HOST} !^mydomain.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).mydomain.com [NC]
RewriteRule ^$ /index.php?username=%2 [QSA]
But I couldn't pass other variables.
something like ...
thurein.mydomain.com/photos/1
what I want is that url re write to ..
index.php?username=thurein&page=photos&id=1
Thanks
If you can help me .. I do really appropriate for it.

i do this on my htaccess and it works
RewriteEngine on
RewriteCond %{HTTP_HOST} !^mydomain.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+).mydomain.com [NC]
RewriteRule ^([a-z0-9-]+)/$ /subdomain.php?subdomain=%2&menu=$1 [QSA]
for index.php?username=thurein&page=photos&id=1
RewriteRule ^([a-z0-9-]+)/([0-9]+)/$ /subdomain.php?subdomain=%2&page=$1&id=1 [QSA]
hope this solve your problem.

you can redirect all subdomains to main domain for example on index.php, in the script you can parse the url and assign $_GET['username'] = $parsedSubDomain; and the script will do everything like index.php?username=thurein.
I hope it is clear.
P.S you can do the redirection using cPanel if your hosting supports it, or you need to change httpd config.

Related

htaccess redirect with full url

Is it possible to redirect from one domain to another using htaccess?
RewriteRule http://www.google.com/old-route http://www.google.com/new-route [R=301,L]
RewriteRule https://www.google.com/old-route https://www.google.com/new-route [R=301,L]
If not, how would you do the redirect for multiple domains on one project?
This can be achieved by using rewriteConditions: https://wiki.apache.org/httpd/RewriteCond
Something along the lines of:
RewriteCond %{HTTP_HOST} ^site1
RewriteRule ^old-route$ https://www.site1alt.com/new-route [R=301,L]
For one Domain use.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.google\.com/old-route$ [NC]
RewriteRule ^(.*)$ http://www.google.com/new-route [R=301,L]
For redirecting everything you could use this.
Redirect 301 / http://www.google.com/new-route
Given https://www.example.com/foobar the single %{HTTP_HOST} as used in the other answers only looks for www.example.com, not taking the following path into account. So these answers were not working for me. Maybe something changed during Apache versions. What worked for me was to concatenate %{HTTP_HOST} and %{REQUEST_URI} to be checked.
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www\.example\.com/foo\.html$ [NC,OR]
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www\.example\.com/bar\.html$ [NC]
RewriteRule ^(.*)$ https://www.example.com/foobar [R=301,L]
Check out this nice Mod_Rewrite Variables Cheatsheet for an overview of many available mod_rewrite variables.

url rewrite and redirect - php

I recently developed a web site using php which accepts a id as its input via get.
http://website.com/profile.php?id=123. its the old fashion way, I've seen sites use pretty url's like http://website.com/username.html
So in order to archive this, i managed to do some research and end up with this code blow.
Also i'm passing the username in the query string.
http://website.com/profile.php?id=123&username=test_user
the parameter ID is only used as a input for the profile.php
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /profile.php [NC]
RewriteCond %{QUERY_STRING} ^id=([^&]+)&username=([^&]+) [NC]
RewriteRule ^.* http://%{HTTP_HOST}/%2.html [R=301,L]
The final output i seek is http://website.com/username.html
Is their anything wrong in this ? this doesn't seem to work. Am i doing something wrong ? if so please be kind to let me know where my error lies. Thank you.
Try this rule:
RewriteEngine On
RewriteRule ^profile/([^/\.]+)/([^/\.]+).html/?$ profile.php?id=$1&username=$2 [L]
then you should be able to use
http://website.com/profile/123/username.html
as the URL. You can ignore the profile part but I don't recomment it because it would clash with other URLs of your website.
If you only need the username, use
RewriteRule ^profile/([^/\.]+).html/?$ profile.php?username=$2 [L]
and the URL would be
http://website.com/profile/username.html
You can use this rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} /profile.php\?id=([^&]+)&username=([^&]+) [NC]
RewriteRule ^ /%1/%2.html? [R=301,L]
RewriteRule ^([0-9]+)/([^/.]+)\.html$ profile.php?id=$1&username=$2 [L,QSA,NC]

.htaccess if request url diffrent from some string than

i have a trivial question about htaccess rewrite condition.
The think i want to do in my htaccess is:
When someone accesses my project from a url different from "example.com" to be redirected to a specific controller, for example app_user.php
I need the syntacs for this condition redirect.
Thanks for your time
Try this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/app_user.php
RewriteRule ^ /app_user.php [L]

.htaccess Remove PHP get data from URL

Well I'm making profile pages so right now it looks like this
http://example.com/random/?user=Robert
What I want to do is remove ?user= from the URL so the page appears as
http://example.com/random/Robert
Iv'e searched and I can't find anything working for me.
Thanks!
Based on http://statichtml.com/2010/mod-rewrite-baseon-on-query-string.html this should do the trick:
RewriteCond %{QUERY_STRING} ^user=(.*)$ [NC]
RewriteRule ^random$ random/$1 [NC,L,R=301]
The first step is to make all of your links in this form http://example.com/random/Robert, then in the htaccess file in your document root, add:
RewriteEngine On
RewriteRule ^random/(.+)$ /random/?user=$1 [L]
But to handle 301 redirects to your old URLs, you can include this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /random/\?user=([^\ ]+)
RewriteRule ^random/$ /random/%1 [R=301,L]

Rewrite subdomain but keep url

I have a website at domain.com, it has a login page at domain.com/user/login.
I would like login.domain.com to show the login but still have the url login.domain.com.
what i have now:
RewriteCond %{HTTP_HOST} ^login.domain.nl$ [NC]
RewriteRule (.*) http://domain.nl/user/login$1 [L]
but this changes the url as well and if i change it to:
RewriteCond %{HTTP_HOST} ^login.domain.nl$ [NC]
RewriteRule (.*) http://domain.nl/user/login$1 [P,L]
i get a 400..
what am i doing wrong?
This question is in the wrong seciton, however, you can't necessarily call the remote page like you want to (even using the proxy flag), you need to make a local reference.
You need to do something like:
RewriteCond %{HTTP_HOST} ^login.domain.nl$ [NC]
RewriteRule (.*) /user/login.php?arguments=$1 [QSA,L]
That references the local location of the file.

Categories