Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

mod_rewrite redirects to local filesystem path

Posted on 2009-02-17
12
Medium Priority
?
632 Views
Last Modified: 2012-05-06
Hi there,

just started experimenting with mod_rewrite and I need some help due to lack of time.  I've got Apache installed locally and this is where I conduct my tests.  I am using Apache 2.2 and Windows XP.  I use mod_rewrite through an .htaccess file.

Currently, my local site has an address of http://localhost/mysite/.  The respective production site has a http://www.mysite.com address and I want both to work without different modifications to the .htaccess file.  All tests below are conducted in the local site.  My issue is this:

RewriteRule ^(.*)index.php en/welcome/ [C]
RewriteRule ^([^/\.]+)/welcome/?$ index.php?lng=$1 [L]

The purpose of the above is to redirect http://localhost/mysite/index.php to http://localhost/mysite/en/welcome/

This seems to work correctly as long as I keep the [C], but as soon as I change it to [R] or [R=301] (I want this to be a 301 redirect in order to keep page rank in the live site), the URL includes a local file system path in my browser as follows:

http://localhost/D:/My%20Documents/Work%20Repository/Development/Web/mysite/en/welcome/

D:/My%20Documents/Work%20Repository/Development/Web/ is the folder where I have my local sites.  Because I just started, I don't know if there is something wrong with my .htaccess code or if this is a configuration issue that must be dealt with another way.  I got the same issue for RewriteRule ^([^/\.]+)/projects/$ $1/projects/10/ [C] which tries to redirect http://localhost/mysite/en/projects/ to http://localhost/mysite/en/projects/10/

I include my whole htaccess file in the Code section. Thanks! :)
RewriteEngine On
RewriteRule ^(.*)index.php en/welcome/ [C]
RewriteRule ^([^/\.]+)/welcome/?$ index.php?lng=$1 [L]
RewriteRule ^([^/\.]+)/projects/$ $1/projects/10/ [C]
RewriteRule ^([^/\.]+)/projects/([^/\.]+)/?$ projects.php?lng=$1&id=$2 [L]
RewriteRule ^([^/\.]+)/services/?$ services.php?lng=$1 [L]
RewriteRule ^([^/\.]+)/contact/?$ contact.php?lng=$1 [L]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule ^.+\.php$ /notfound
Options -Indexes
DirectoryIndex index.php

Open in new window

0
Comment
Question by:acicovic
  • 6
  • 6
12 Comments
 
LVL 27

Expert Comment

by:caterham_www
ID: 23665425
Try to set the RewriteBase directive in case the prefix-stripping from the substitution fails:

RewriteBase /mysite/
0
 
LVL 2

Author Comment

by:acicovic
ID: 23668612
Hi caterham_www,

First of all thank you for your reply.  This works on my local server, but I guess it won't on the live site.  Is there any other way that can make it work globally?

Thanks...
0
 
LVL 27

Expert Comment

by:caterham_www
ID: 23668857
You need to change the RewriteBase on your live server. A .htaccess file is a per-directory configuration file in addition to your httpd.conf on *your* server, not for any server.

Rebuilt your test server to a system which is 1:1 identical to your live site if you require to copy everything without changing some files.
0
Industry Leaders: 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 2

Author Comment

by:acicovic
ID: 23669159
On my first reply I was using RewriteBase within my .htaccess.  This doesn't seem to be what I want though...

Is there perhaps a way I can use RewriteBase in httpd.conf to specificly redirect D:/My%20Documents/Work%20Repository/Development/Web/mysite/ to /mysite/ ?  

If I can do it within httpd.conf then I wouldn't have to change the .htaccess file and the problem would be solved...
0
 
LVL 27

Expert Comment

by:caterham_www
ID: 23696575
That would be somewhat ugly, but...
Redirect "D:/My Documents/Work Repository/Development/Web/" http://localhost/

Open in new window

0
 
LVL 2

Author Comment

by:acicovic
ID: 23716394
Hi there,

I just tried it, and to my amazement it doesn't work. I'll see if I can conduct some more testing tomorrow and I'll let you know if I have more information.
0
 
LVL 27

Expert Comment

by:caterham_www
ID: 23716572
May be we need the leading slash:

"/D:/My Do....
0
 
LVL 2

Author Comment

by:acicovic
ID: 23739340
Unfortunately It doesn't work, with or without the slash.  Any other ideas?
0
 
LVL 27

Accepted Solution

by:
caterham_www earned 1000 total points
ID: 23739513
(code below)

in the main server configuration file httpd.conf works for me and redirected a request of

http://localhost/D:/My%20Documents/Work%20Repository/Development/Web/test/index.php  to http://localhost/test/index.php.
Redirect "/D:/My Documents/Work Repository/Development/Web/" http://localhost/

Open in new window

0
 
LVL 2

Author Comment

by:acicovic
ID: 23759653
Hi there,

How would I go about adding this in httpd.conf?  Do I need any VirtualHost declarations or do I just put it in there?
0
 
LVL 27

Expert Comment

by:caterham_www
ID: 23759856
Just put it in there, may be at the bottom of the file and restart apache afterwards.
0
 
LVL 2

Author Comment

by:acicovic
ID: 23777739
Thanks!  It works now :)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Loops Section Overview
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

572 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