Solved

Apache rewrite with variable domain

Posted on 2009-04-09
6
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions. For instance redirecting http://domain.com (http://domain.com) to http…
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

627 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