Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Apache rewrite with variable domain

Posted on 2009-04-09
6
Medium Priority
?
270 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 2000 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Configure Web Service (server application) I. Configure security for Web Services methods First, we need to protect Session bean which implements the service: 1. Open EJB deployment descriptor (ejb-jar.xml) in the EJB project that contains you…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

715 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