dimaj Posted January 22, 2011 Share Posted January 22, 2011 Hey guys! I've got a question for you about using mod_rewrite with apache2 A router that forwards port 1234 to port 80 on an internal IP. I have setup a web redirect (via dyndns) to forward subdomain.domain.com to domain.com:1234/subdomain. Every time I navigate to subdomain.domain.com, it automatically changes my url (in the browser) to domain.com:1234/subodmain which looks very ugly and unprofessional. I was wondering if I could user mod_rewrite to forcefully overwrite the url to subdomain.domain.com at all times. Any help is appreciated. dimaj Quote Link to comment Share on other sites More sharing options...
Infiltrator Posted January 22, 2011 Share Posted January 22, 2011 (edited) I am not very certain whether you can achieve what you want with Mod_rewrite. Mod_rewrite its only meant for rewriting the last part of the URL. For example: Take note of this URL, http://www.pets.com/show_a_product.php?product_id=7 With mod_rewrite you can substitute it to something like this http://www.pets.com/products/7/ But what you need is a DNS server and an A record added, that points to the subdomain.domain.com. Edited January 22, 2011 by Infiltrator Quote Link to comment Share on other sites More sharing options...
dimaj Posted January 22, 2011 Author Share Posted January 22, 2011 (edited) I am not very certain whether you can achieve what you want with Mod_rewrite. Mod_rewrite its only meant for rewriting the last part of the URL. For example: Take note of this URL, http://www.pets.com/show_a_product.php?product_id=7 With mod_rewrite you can substitute it to something like this http://www.pets.com/products/7/ But what you need is a DNS server and an A record added, that points to the subdomain.domain.com. Thanks for the fast reply! Mod_rewrite can rewrite any part of the url, or at least that's my understanding from reading this: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html As far as DNS concerned, I have the following setup: A-record: mydomainname.com <my external ip address> WebHop: blog.mydomainname.com mydomainname.com:1234/blog and a whole bunch of CNAME records. To tell you the truth, I'm not that knowledgeable of DNS as a whole. What exactly will an A-Record help me accomplish? EDIT: Web server that I'm trying to access, hosts several applications: forum, wiki, blog, etc. So, I can access those different apps by using the following format: http://mydomain.com:1234/appName and I want to change it http://appName.mydomain.com EDIT 2: As I just found out, WebHop is some sort of an A record. Thanks again. dimaj Edited January 22, 2011 by dimaj Quote Link to comment Share on other sites More sharing options...
Infiltrator Posted January 22, 2011 Share Posted January 22, 2011 (edited) Since I haven't used Mod_rewrite before and I must admin its kind of complex to understand at first, but once you do it a couple of times you begin to understand how it all comes together. I've experimented with other mods in the past, but not Mod_rewrite. I think I am going to fire up some virtual machines and have a crack at your problem. Will let you know when I have come up with a solution. In the meantime, may be someone else can shed some light for your problem. Edited January 22, 2011 by Infiltrator Quote Link to comment Share on other sites More sharing options...
Sparda Posted January 22, 2011 Share Posted January 22, 2011 You can't remove the port (http://domain.com:1234/something) without braking the website. The browser needs to know that the server is running on a different from default port. If sounds like what you have setup with dyndns is not a domain record as such (well, it is partly) but a 301 or 307 redirect, so the browser literally goes and gets another URL. If you want to get rid of the port you have to run the server on port 80. To setup the subdoamin correctly you need to create a new virtual host in your Apache config that defines it. mod_rewrite can't do any of this this. Quote Link to comment Share on other sites More sharing options...
Alias Posted January 22, 2011 Share Posted January 22, 2011 I would use Nginx instead of Apache. It's lighter than Apache and can do this sort of stuff really easily. Although it might be a bit of a challenge to convert your rulesets from Apache to Nginx once you get the hang of it you really begin to realise the power that Nginx has. Once my server comes back online again I'll test this out for you but this is what I reckon you'll need to do what you need to do. server { listen 1234; server_name _; access_logs /var/log/nginx/domain2.com.access.log main; root /var/www/domain.com; index index.php index.html index.htm; # Some PHP support? Check these parameters. location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/domain.com$fastcgi_script_name; include /etc/nginx/fastcgi_params; } } server { listen 80; server_name ~^(.*)\.domain\.com$ ; access_logs /var/log/nginx/domain.com.access.log main; location / { proxy_pass http://domain.com:1234/$1 break; } } Note that you will have to have A records for each subdomain. An A record takes a human recognisable name and converts it into an IP Address. For example what you will need to setup is an A record for webhop.yourdomain.com, then Nginx will interpret that request at port 80, take the subdomain and the proxy_pass it to yourdomain.com:1234/webhop. Hopefully that should work (yeah right) Quote Link to comment Share on other sites More sharing options...
dimaj Posted January 22, 2011 Author Share Posted January 22, 2011 Thanks for your replies guys! I'm actually inclined to agree with Sparda right now since I have a router between my web server and the internet. Basically, in order for me to make it work, my router will have to be running apache or Nginx and rewriting rules there or just setup a port forwarding for port 80 instead of port 1234. Does that sound correct? Quote Link to comment Share on other sites More sharing options...
Sparda Posted January 22, 2011 Share Posted January 22, 2011 Your router doesn't (and shouldn't) run a web site that is exposed to the internet. Just forward port 80 instead of 1234. Quote Link to comment Share on other sites More sharing options...
dimaj Posted January 22, 2011 Author Share Posted January 22, 2011 Your router doesn't (and shouldn't) run a web site that is exposed to the internet. Just forward port 80 instead of 1234. True... I guess that's the best solution. Can I still rewrite the url from domain.com/app to app.domain.com? Thanks! dimaj Quote Link to comment Share on other sites More sharing options...
Sparda Posted January 22, 2011 Share Posted January 22, 2011 No, you need to setup a virtual host in Apache for app.domain.com. mod_rewrite is used to internally redirect the request to another file or directory based on a rule set, it can't change the URL displayed in the browser. Quote Link to comment Share on other sites More sharing options...
dimaj Posted January 22, 2011 Author Share Posted January 22, 2011 No, you need to setup a virtual host in Apache for app.domain.com. mod_rewrite is used to internally redirect the request to another file or directory based on a rule set, it can't change the URL displayed in the browser. Got it! Thanks for the help! dimaj Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.