Solved

Apache rewrite with variable domain

Posted on 2009-04-09
6
264 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Website being blocked? 3 127
browser caching 4 72
Apache Issues 9 81
How to redirect all users but me properly to another page with htaccess 2 96
If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
There are numerous questions about how to setup an IBM HTTP Server to be administered from WebSphere Application Server administrative console. I do hope this article will wrap things up and become a reference for this task. You need three things…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 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