Solved

Apache rewrite with variable domain

Posted on 2009-04-09
6
262 Views
Last Modified: 2013-12-02
I need to rewrite a URL from host/index.php to host/.

There are various solutions out there however they rely on the host being a constant, such as (1) below. When I try to make a simple adjustment to cater to a variable host such as (2) below I run into trouble.

Any help greatly appreciated.
(1)

RewriteCond ${REQUEST_URI} ^/index.php$

RewriteRule .* http://host/ [L,R=301]
 

(2)

RewriteCond ${REQUEST_URI} ^/index.php$

RewriteRule .* ${HTTP_HOST} [L,R=301]

Open in new window

0
Comment
Question by:RegProctor
  • 3
  • 3
6 Comments
 
LVL 27

Expert Comment

by:caterham_www
ID: 24106249
You're using the rule in your httpd.conf?

That should be enough:
# httpd.conf

RewriteEngine on

RewriteRule ^/index\.php$ http://%{HTTP_HOST}/ [L,R=301,NS]
 

# .htaccess:

RewriteEngine on

RewriteRule ^index\.php$ http://%{HTTP_HOST}/ [L,R=301,NS]

Open in new window

0
 
LVL 1

Author Comment

by:RegProctor
ID: 24112127
no, in .htaccess. I use the same file for the production server (a web hosting company server), my development server & outside access to my clients of my staging server. The the last two cases is the same server and DB, just a different Virtually hosted URLs with different PHP code (one URL accessible only locally for me and the other from the web.)

That's very close but if I add a query string it still removes the index.php. In the case of an existing query string the file name should remain so you don't get something like:

/host/?a=b
0
 
LVL 27

Accepted Solution

by:
caterham_www earned 500 total points
ID: 24112614
You'll need a condition in order to check for an empty query string
RewriteEngine on

RewriteCond %{QUERY_STRING} =""

RewriteRule ^index\.php$ http://%{HTTP_HOST}/ [L,R=301,NS]

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:RegProctor
ID: 24113869
Works like a charm  - thanks!

What through me was when I did

RewriteRule .* ${HTTP_HOST} [L,R=301]

Apache through up the system's file path instead of the host name. You showed that you needed to add http:// in front of it. I didn't think of that as it just looked like it was completely ignoring the HTTP_HOST instruction. By the way, do you know why it would do that?
0
 
LVL 27

Expert Comment

by:caterham_www
ID: 24114585
You used ${... while the variable needs to be referenced via %{..., that's why nothing happened.
0
 
LVL 1

Author Comment

by:RegProctor
ID: 24114635
Sorry, typo, it was a %{... not a ${... you'll see it correct at the top. However, it's not that nothing happened, that's easy, it's that I got the file path, like... /home/name/www/... when I was expecting a URL.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Most of the developers using Tomcat find it easy to configure the datasource in Server.xml and use the JNDI name in the code to get the connection.  So the default connection pool using DBCP (or any other framework) is made available and the life go…
Upgrading Tomcat – There are a couple of methods to upgrade Tomcat is to use The Apache Installer is to download and unzip and run the services.bat remove|install Tomcat6 Because of the App that we are working with, we can only use Tomcat 6.…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now