Jump to content

Issue with mod_rewrite and internal redirect


Psycho
 Share

Recommended Posts

I don't have any prior experience with mod_rewrite and am trying to create internal redirects to create a test harness for an application. I am using Apache on an XAMPP install. I have enabled mod_rewrite by modifying the conf file. I can get some rules to work that will modify the URL in the browser (i.e. external redirect), but I cannot get ones to work that do an internal redirect.

For example, if I have two files: foo.html and bar.html

If I use a Redirect in my .htaccess file

RewriteEngine  on
Redirect "/foo.html" "/bar.html"

Then attempting to load localhost:8080/foo.html is redirected in the browser address bar and in the loaded content. However, what I want to happen is to enter the URL for foo.html and only see the content for bar.html - i.e. the address in the browser should still display foo.html. Based on the examples, I've seen I need to use a RewriteRule. I have tried a couple different ways to do this (see below) based on info at apache.org. But when I do, I still get the content for foo.html. It's as if the RewriteRule is not doing anything.

Here is the format I think I should be using so the url in the browser is not changed:

RewriteEngine  on
RewriteRule    "^/foo\.html$"  "/bar.html" [PT]

Here is another version using RewriteRule, that I think should work just like Redirect (which is working)

RewriteEngine  on
RewriteRule    "^/foo\.html$"  "bar.html"  [R]

But, as stated above, neither of these work - I just get the content for foo.html (and the URL is unchanged). Any ideas?

 

FWIW: Here is the actual logic I want to achieve using a RedirectMatch but, as stated above, I want to do with as an internal redirect which is hidden from the calling agent.

RedirectMatch "/data/FAMStaticData/(.*).txt(.*)" "http://localhost:8080/index.php?file=$1"

 

Link to comment
Share on other sites

Here you go

  • Line 1 enables the rewrite engine.
  • Line 2 contains the RewriteRule directive:
  • The pattern ^old.html$ matches the page which starts (^) and ends ($) with old.htm.
  • The substitution is the new.htm page.
  • The flag [NC] means the pattern is not case-sensitive.
  • When a user tries to access the old page, the server shows the contents from the new.htm page instead.

 

RewriteEngine On
RewriteRule ^old.htm$ new.htm [NC]

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.