.htaccess rewrite a subdomain with a variable - .htaccess

Is this possible point a subdomain to specific variable?
http://whatever.example.com point to http://example.com/file.php?var=1
http://whatever1.example.com point to http://example.com/file.php?var=2
Tried the recommended answer below - the code work but no css style:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?whatever\.example\.com$
RewriteRule ^ /file.php?var=1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(?:www\.)?whatever1\.example\.com$
RewriteRule ^ /file.php?var=2 [L,QSA]
or
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.example\.com$
RewriteRule ^ /file.php?var=%1 [L,QSA]
Got it working by adding <base href=""/>in header and making a folder called cssfolder for my css because root folder wasn't working. I dont know why?
<base href="../" /> "not working"
<base href="/" /> "not working"
<base href="./" /> "not working"
<base href="/cssfolder/" /> "this work"

Sure that is possible. But you need to tell a few things apart:
The actual host name in the URL ("subdomain") is not part of the path a RewriteRules pattern is internally matched against. You need to use a RewriteCond to access that information:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?whatever\.example\.com$
RewriteRule ^ /file.php?var=1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(?:www\.)?whatever1\.example\.com$
RewriteRule ^ /file.php?var=2 [L,QSA]
A typical variant would be to hand over an actual name instead of a numeric value. Here the GET variable would become v=whatever or v=whatever1:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.example\.com$
RewriteRule ^ /file.php?var=%1 [L,QSA]
This assumes that all hosts/subdomains are served from by same http server, of course, since it is an internal rewrite.
In general I would recommend that you take a look at the documentation of the tools you want to use. That documentation is, as typical for OpenSource software, of excellent quality and comes with great examples: https://httpd.apache.org/docs/current/mod/mod_rewrite.html

Related

htaccess rewrite to wildcard subdomain explained

dear community.
Today i made this .htaccess rewrite rules ( found some part on the internet and added some by myself )
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^(.*).site.com/(.*)$
RewriteRule ^ %1.php
When i request some.site.ru - site.ru/some.php content triggered. It's fine. And when i request some.site.ru/readme.txt i see site.ru/readme.txt content. And i dont quite understand why its working fine.
I thought RewriteRule ^ %1.php makes all requests to go through *.php files. Am i wrong?
Additionally i cant understand RewriteRule ^ this part. Is it the same as RewriteRule ^(.*)$ ?
This is your rule:
RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^(.*).site.com/(.*)$
RewriteRule ^ %1.php
Due to presence of faulty pattern in front of RewriteCond %{HTTP_HOST} it is a DO NOTHING rule, in other words this rule will never execute because of presence of a / in 2nd condition which can never be true because %{HTTP_HOST} matches only the host name part of a web request. So %{HTTP_HOST} can only match www.example.ru or example.ru.
So it is obvious that /readme.txt URL is working fine without any rewrite.

RewriteRule not working for multi URL

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} ^[a-zA-Z0-9-+/]{3,}\ /+company\.php\?title=([^\s&]+)
RewriteRule ^ %1? [R=301,L]
RewriteCond %{THE_REQUEST} ^[a-zA-Z0-9-+/]{3,}\ /+project\.php\?compname=([^\s&]+)&prjt=([^\s&]+)
RewriteRule ^ %1/%2? [R=301,L]
RewriteRule ^([^/]+)/?$ company.php [L]
RewriteRule ^([^/]+)/([^/]+)/?$ project.php [L]
what I expect is http://website.com/compname=Company&prjt=Project should look like http://website.com/Company/Project. And the same way company.php page also. Company.php page s working as expected, But project.php page is not working. Url is showing as expected, But page opening with complete errors. Any help is highly thankful?
If you're missing styles and scripts, your problem is most likely that you're using relative links in your content. When you change the URL from
/project.php
to
/company/project
any relative URL's will have the base change from / to /company/, which is obviously not what you want.
Either make all your links absolute URLs that start with a / or add a base to your page headers:
<base href="/" />
Company page is coming here without style sheet (messy look).
That's what I was asking you. We need to know the specific problem because opening with errors is not the same as your style sheet is missing.
Fix your stylesheet problem by adding this tag in your <head> section.
<base href="http://www.website.co" />
This happens when using this type of mod_rewrite and relative URL's in your html for CSS/JS etc.

.htaccess, Rewriterule not working as i want

I have this link: http://www.domain.com.mk/lajmi.php?id=2790,
and i want to change it to http://www.domain.com.mk/lajmi/2790
With this code I can change the link to /lajmi/2790 but i get 404 error.
I mean i get the link
http://www.domain.com.mk/lajmi/2790, but it has 404 error (i dont se the content)
This is my code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com\.mk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\.mk$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^lajmi\.php$ http://domain.com.mk/lajmi/%1? [R=302,L]
What I am doing wrong ?
Try this one :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteCond %{QUERY_STRING} ^id=(\d*)$
RewriteRule ^lajmi\.php$ http://domain.com.mk/lajmi/%1? [R=302,L]
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1&r=0 [L]
(the &r=0 in the final rule is for not getting an infinite loop)
Single direction rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1 [L,QSA]
This means that every uri of kind /lajmi/2790 will be passed to /lajmi.php?id=2790 in a sub-request.
However, in this case, if the user hits /lajmi.php?id=2790 by himself, then this is the url he will see in the browser, not the "beautified one".
Bi-directional rewrite:
RewriteEngine On
RewriteBase /
; Redirect lajmi.php?id=2790 to a beutified version, but only if not in sub-request!
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteCond %{IS_SUBREQ} !=true
RewriteCond %{QUERY_STRING} ^id=(\d*)$
RewriteRule ^lajmi\.php$ lajmi/%1 [R=301,L]
; Make the beutified uri be actually served by lajmi.php
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com\.mk$
RewriteRule ^lajmi/(\d*)$ lajmi.php?id=$1 [L]
Here, an additional RewriteCond was added to the first rule checking that this is not a sub-request, to ensure that the rules do not loop.
You can pick which way you like, but the first approach is enough if you build the links in your HTML in the 'beautified' way already (no need to redirect the browser twice just to see the page).

How do I get the ENTIRE URL as seen in the browser without JS?

I have an application I'm building in ColdFusion, whereby all requests will run through the index.cfm file.
I have a .htaccess file that rewrites the URL. So, for example...if I write:
http://domain.com/hello/goodbye/howdy
The actual request always uses index.cfm like so:
http://domain.com/index.cfm/hello/goodbye/howdy
This all works great, but now I'm stuck with how I can grab everything that is in the URL. Not one of the CGI variables don't seem to output the "/hello/goodbye/howdy" part of the URL.
I have tried cgi.path_info and cgi.query_string etc to no avail...they're just blank.
I need to grab everything that comes after the domain name, and do stuff in CF with it. I know it's possible in JS, but I really need this on the server.
Dumping the CGI scope shows me nothing useful in this regard:
<cfdump var="#cgi#" />
Here's my htaccess file for reference:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.cfm$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.cfm [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^([^\.]+)\.domain\.com
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
Thanks.
EDIT:
As an additional note, I've also tried the underlying Java methods like so:
<cfdump var="#getPageContext().getRequest().getContextPath()#" />
<cfdump var="#getPageContext().getRequest().getRequestURL()#" />
<cfdump var="#getPageContext().getRequest().getQueryString()#" />
To no success :(
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.cfm$ - [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^([^\.]+)\.domain\.com
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
#Change exists here:
RewriteRule ^(.*)$ /index.cfm?actualuri=$1 [L,QSA]
</IfModule>
try cgi.query_string now. It should have actualuri=/the/path/sent.
Also, put the rewrite rules in the same order as put above.
Check #CGI.REQUEST_URI# - it's undocumented but works
I hope this is what you are looking for.
<cfset link = "http://" & GetHttpRequestData().headers['host'] & GetHttpRequestData().headers['X-REWRITE-URL'] >
I think the simplest means is to actually look at the CGI.PATH_INFO field.

Weird .htaccess url rewrite discrepancy

So http://myopicvoid.org/ when loaded in Firefox or Chrome, automatically redirects to http://myopicvoid.org/main as well it should, but not in IE8. What exactly would cause this? My .htaccess is as follows:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^myopicvoid\.org$
RewriteCond %{REQUEST_URI} ^/$
RewriteRule / /main [r=301,L]
RewriteCond %{HTTP_HOST} ^myopicvoid\.org$
RewriteCond %{REQUEST_URI} !^/script/.*
RewriteCond %{REQUEST_URI} !^/style/.*
RewriteRule ^(.*)$ /script/$1.py [L]
There exists a main.py in /script, but I'm met with Error 404 Not Found (script not found or unable to stat: script/.py) in IE. Help?
I would recommend changing
RewriteCond %{HTTP_HOST} ^myopicvoid\.org$
RewriteCond %{REQUEST_URI} ^/$
RewriteRule / /main [r=301,L]
To:
RewriteCond %{HTTP_HOST} ^myopicvoid\.org$
RewriteRule ^/?$ /main [r=301,L]
So, what I've done is removed the condition, but made the rule only match either nothing, or just a single fore-slash. The condition wasn't really necessary; it's important to note that mod_rewrite processes rules first, then checks to see if they meet their conditions, so this should be a tiny bit more efficient.
This will be a little more forgiving if the request does not include the trailing slash.
Off the top of my head - maybe the URL http://myopicvoid.org/ has the trailing slash removed when you make the request from certain browers? This would prevent the "/" from matching the first RewriteRule.

Resources