rewrite htaccess subdomain - .htaccess

Mysite's running normally with the code rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ https://www.mysite.com/$1 [L,R=301]
#rewrite www.mysite.com/page.php?page=ID => www.mysite.com/ID
RewriteRule ^([a-zA-Z0-9_-]+)/?$ page.php?page=$1
It's running okay, but if the ID has a dot (.), it's not working? So
how do I fix this?
#rewrite www.mysite.com/game/play.php?g=IDgame => www.mysite.com/game/IDgame
RewriteRule ^game/([a-zA-Z0-9_-]+)$ /game/play.php?g=$1
It's working fine :)
I add subdomain for mysite is: m.mysite.com
I want to rewrite:
m.mysite.com/game/play.php?g=IDgame => m.mysite.com/game/IDgame
How do I code this?

Instead of having a list of characters the id might contain, it is often easier to have a list of characters the id certainly cannot contain. In your case it can most likely not contain a slash. You could therefore match with ([^/]+). You want to explicitely exclude play.php from matching as well, which you can do with a rewrite condition.
RewriteCond %{REQUEST_URI} !/game/play\.php
RewriteRule ^game/([^/]+)/?$ /game/play.php?g=$1
As for your m.mysite subdomain. Make sure that the virtual host for both www.mysite and m.mysite point to the same directory. If your dns is configured properly it should internally rewrite your mobile game urls to the same file.

Related

friend url on wildcard subdomains htacces, not working

i have wildcard subdomains sets already and works fine, now i wish have friends url for the content in thats subdomains, the structure of my site is if the user type subdomain.maindomain.com and the .htaccess redirect to
blogs/index.php?user=subdomain
where blogs/index.php receive the param and show the correct content
now i try to make the url function like this
subdomain.maindoamin.com/24/title-of-content
and then .htaccess must result
blogs/index.php?id_content=24&title=title-of-content
i have the next .htaccess
Options +FollowSymLinks
#this force to server the content always without www.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ http://%1/$1 [R=301]
#this is to pass the subdomain like param and show the right content of the user
RewriteCond %{HTTP_HOST} !^www\.misite\.com [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.misite\.com
RewriteRule ^(.*)$ blogs/index.php?url=%1 [QSA,L]
#the next line i can't make work to make nice url
RewriteRule ^/(.*)/(.*)$ blogs/index.php?idP=$1&name=$2 [L]
not working because when i make in index.php
echo $_SERVER['REQUEST_URI'];
don't show idP=24 show /24/title-of-content and i need $_GET(idP)
i really apreciate some light on this stuff i am not expert on htaccess, thanks in advance to everybody.
There are two problems:
The first argument of RewriteRule matches against everything after the slash of the directory .htaccess is in, and before the query string. If .htaccess is in your www-root, and you get the url http://www.example.com/shiny/unicorns.php?are=shiny, you match against shiny/unicorns.php. It will never start with a slash, so ^/ will never match.
Rules are executed in order. If you go to http://sub.example.com/10/unicorns, the second rule will match first and rewrite the request to /blogs/index.php?url=10/unicorns. If you removed the leading slash the third rule would match, but normally you wouldn't want that. You want to have the third rule only match
You want to move the third rule up so it is the second rule. You want to make it more specific to only match with subdomains. You also know the first part contains only numbers, so use that knowledge to prevent blogs/index.php from matching your now second rule. You also need to prevent blogs/index.php from matching the now third rule to prevent it from matching itself. Last but not least I removed [L] from the now second rule, since the third rule will match anyway.
#the next line i can't make work to make nice url
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^([0-9]+)/([^/]+)$ blogs/index.php?idP=$1&name=$2
#this is to pass the subdomain like param and show the right content of the user
RewriteCond %{HTTP_HOST} !^www\.misite\.com [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9]+)\.misite\.com
RewriteCond %{REQUEST_URI} !/blogs/index\.php
RewriteRule ^ blogs/index.php?url=%1 [QSA,L]

htaccess subdomain rewrite without a redirect

Using htaccess Rewrite, I want my url http://*.phoneataxi.com/ (where * is a wildcard, excluding 'www') to show in the address bar as is but get information from http://*.phoneataxi.com/test.php?c=*.
I have tried so many different things but nothing is doing exactly what I need. Most examples are redirecting the subdomain to the '/test.php' file in the address bar which I don't want to do.
I'm trying not to have to create individial subdomains and subdomain folders within my webroot.
Ideas?
I use this htaccess file to make Apache act as a proxy for another host:
IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^ghost\.pileborg\.se$
RewriteRule (.*) http://vps.pileborg.se/ghost/$1 [P]
</IfModule>
It causes all access to http://ghost.pileborg.se/ to be "redirected" to http://vps.pileborg.se/ghost/.
UPDATE (2020)
Some of the answers regarding this topic is very old and no longer work as expected.
After searching for hours on something that actually works, this is what I came up with; edit as you see fit:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ([a-z0-9]+)\.
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}/index.php -f
RewriteRule ^(.*)$ %{ENV:BASE}/index.php [L,NC,QSA]
RewriteCond %{DOCUMENT_ROOT}/%{ENV:BASE}/index.html -f
RewriteRule ^(.*)$ %{ENV:BASE}/index.html [L,NC,QSA]
Breakdown
Make sure that the rewrite module is installed and enabled on your host
first we turn the rewrite engine on and set the path-base
then isolate the subdomain - any letters/numbers before the first dot
set a variable in this runtime environment that contains the subdomain
check if the subdomain folder and index-file exists
if it does exist -then use that file as the request-handler (no redirect)
if it does not exist then the request carries on normally
Flags
The flags used here are explained here, but the ones used above are quite simple:
[L] Last rule, ignore the rest
[NC] No Case, no uppercase/lowercase restrictions
[QSA] I remember this as "Query String Attach" :D

htaccess redirect multiple domains to central domain

Hi if have 3 domain names with a dns that point at one central site with trough a central htaccess file.
I would like to redirect the site by dns to the central site + subdirectory containing the domain name.
for example:
testsite1.com => thesite.com/testsite1/
testsite2.com => thesite.com/testsite2/
testsite3.com => thesite.com/testsite3/
my idea
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.bla1.com$ [AND]
RewriteCond %{HTTP_HOST} !^bla1.com$
RewriteRule ([^.]+)\.com(/[*]) http://bla2.com/$1/$2 [R=301,L]
The thing is, RewriteRule is not applied to a HTTP_HOST, only on a REQUEST_URI.
So You can do this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?example.com$
RewriteRule (.*) http://example.com/%{HTTP_HOST}/$1 [L,R,QSA]
Note however, that ".com" will not be stripped from the hostname that is passed in the new path
Btw, "301" is "Moved Permanently". I am not sure You want that, but anyway, it is a common good practice to first use 302 status when redirecting until you're done testing and everything is finalized

htaccess redirects from URLs on one domain to the root of another domain

Currently what is happening is people are accessing old URLs from google like icpaweb.com/site/pages/about-us/ and being sent to their corresponding urls on icpaweb.org : icpaweb.org/site/pages/about-us.
What I want is to send people from: icpaweb.com/site/pages/about-us to icpaweb.org/ without any of the succeeding url segments.
How do I do this?
If you have to use an .htaccess file, you can use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} icpaweb.com$ [NC]
RewriteRule .* http://icpaweb.org/ [R=301,L]
That will 301 redirect all requests for icpaweb.com to the index root of icpaweb.org. If you don't want 301, it can just be R.
You'll need to replace or turn off whatever mechanism is doing your redirecting now, they may not be compatible.
Use an url rewrite rule.
2 steps:
Write a RewriteCond so that the following rewrite rule only apply for url with host being icpaweb.com like RewriteCond %{HTTP_HOST} icpaweb.com$ [NC] The [NC] is for case insensitive match
Write a rewrite rule that convert all input to what you want like RewriteRule ^.*$ http://icpaweb.org/ [L]The [L] is to stop the rewriting to this rule if rule executed.

Rewrite htaccess old oscommerce links

I am trying to rewrite all the old oscommerce links to a new website. But I am having trouble with part of the URL I need to rewrite.
The link looks like this:
http://www.domain.com/product_info.php?cPath=3_72&products_id=129&osCsid=6j3iabkldjcmgi3s1344lk1285
This rewrite works for the above link:
RewriteCond %{REQUEST_URI} ^/product_info\.php$
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129&osCsid=([A-Za-z0-9-_]+)$
RewriteRule ^(.*)$ http://www.domain.com/apple/air.html? [R=301,L]
But will not work for:
http://www.domain.com/product_info.php?cPath=3_72&products_id=129
My problem is that I want the rewrite to work no matter if the &osCsid=6j3iabkldjcmgi3s1344lk1285 part is included or not.
I think you can achieve this by not specifying the closing delimiter ($)
Give this a try:
RewriteCond %{REQUEST_URI} ^/product_info\.php$
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129
RewriteRule ^(.*)$ http://www.domain.com/apple/air.html? [R=301,L]
By not putting the $ at the end of the regex string you are basically saying: match any string that starts with ..., no matter what comes after
Hope this helps :)
This should do the job just fine:
RewriteCond %{QUERY_STRING} ^cPath=3_72&products_id=129
RewriteRule ^product_info\.php$ http://www.domain.com/apple/air.html? [R=301,L]
There is no need for separate condition RewriteCond %{REQUEST_URI} ^/product_info\.php$ -- this part can be (actually, SHOULD BE, for better performance) moved to RewriteRule.
This is enough ^cPath=3_72&products_id=129 -- it tells "When query strings STARTS with ...". No need to include optional/non-important parameters osCsid=([A-Za-z0-9-_]+).
This rule is to be placed in .htaccess file in website root folder. If placed elsewhere some small tweaking may be required.

Resources