Keep Server Online
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
or
A donation makes a contribution towards the costs, the time and effort that's going in this site and building.
Thank You! Steffen
Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
| |
|
Topic: Trouble Getting URL Rewrite to Work |
|
Author |
|
jcobban
Joined: 10 May 2015 Posts: 17 Location: canada, ottawa
|
Posted: Mon 25 May '15 4:06 Post subject: Trouble Getting URL Rewrite to Work |
|
|
On my website at www.jamescobban.net I have a directory which contains an ancient copy of my old static implementation of the site. It is there just in case someone has saved a old URL. However the data in that directory is several years out of date, so I would like to redirect the old static URLs to the equivalent dynamic URLs. For example:
http://www.jamescobban.net/FamilyTree/Web/I@I00197@.html
becomes
http://www.jamescobban.net/FamilyTree/legacyIndivid.php?idir=00197
This should be straightforward, although I feel the documentation could be improved:
I wasn't warned that the <Directory> in the Apache conf file has to specify AllowOverride All whereas the default that is created when I install Ubuntu is AllowOverride None.
I wasn't warned that I have to enable the mod_rewrite using a2enmod or else the rewrite commands are rejected.
I am testing this on my private copy of the web-site where I can fiddle with options like that in the config file, but when I migrate this to my public server I do not have that privilege, so I want to try setting up the URL rewriting in .htaccess.
My .htaccess file is just:
Code: | ErrorDocument 404 /errors/404page.php
RewriteEngine on
RewriteRule ^/FamilyTree/Web/I@I([0-9]+)@.html$ /FamilyTree/LegacyIndivid.php?idir=$1 [L]
|
The ErrorDocument is working and there are no errors reported in the Apache log, so why is the RewriteRule not working? When I enter the old-fashioned URL it continues to display the old static file, not the new dynamic file. |
|
Back to top |
|
glsmith Moderator
Joined: 16 Oct 2007 Posts: 2268 Location: Sun Diego, USA
|
Posted: Mon 25 May '15 8:25 Post subject: |
|
|
Try this instead
Code: | RedirectMatch "/FamilyTree/Web/I@I(.*)@\.html$" "http://www.jamescobban.net/FamilyTree/Web/LegacyIndivid.php?idir=$1"
|
|
|
Back to top |
|
jcobban
Joined: 10 May 2015 Posts: 17 Location: canada, ottawa
|
Posted: Tue 26 May '15 1:00 Post subject: |
|
|
glsmith wrote: | Try this instead
Code: | RedirectMatch "/FamilyTree/Web/I@I(.*)@\.html$" "http://www.jamescobban.net/FamilyTree/Web/LegacyIndivid.php?idir=$1"
|
|
I cannot specify the domain name of the web site, because it is different for my testing environment and the production environment, and the code may be deployed on more than one domain. According to the documentation RedirectMatch is only for the case where the destination is on a different web server. |
|
Back to top |
|
glsmith Moderator
Joined: 16 Oct 2007 Posts: 2268 Location: Sun Diego, USA
|
Posted: Tue 26 May '15 2:03 Post subject: |
|
|
It's not. It's the same as using Redirect except you can use regular expression. Look at Redirect's examples. Quote: | # Redirect to a URL on the same host
Redirect "/one" "/two" |
It works (and without the domain), I tested it first.
Without using the domain the only difference is if LogLevel is set to debug, you'll get a debug message about it being incomplete, which really doesn't make any sense. |
|
Back to top |
|
jcobban
Joined: 10 May 2015 Posts: 17 Location: canada, ottawa
|
Posted: Tue 26 May '15 2:48 Post subject: |
|
|
I obviously have something wrong with my mod_rewrite rules but I cannot tell what it is because the actual actions of mod_rewrite are not visible to me.
But I naturally want to get the rewrite to work on my private development site before I deploy it on the production site. I tried putting a rewrite rule in the document root with absolute URLs but that seemed to do nothing, so I have tried putting a .htaccess using relative URLs in the FamilyTree/Web/ directory.
Code: | RewriteEngine on
RewriteRule ^I@I([0-9]+)@.html$ ../FamilyTree/legacyIndivid.php?idir=$1 [L]
|
This does something, but I cannot tell what, because it gives me a 404 error! How do I determine what URL the server is actually looking for when it returns this error. When I look at $_SERVER['REQUEST_URI'] it still shows the original untranslated URL, which of course still points to a valid page.
I finally got the rewrite working by mapping the relative URL to an absolute URL. So /FamilyTree/Web/.htaccess is now:
Code: | RewriteEngine on
RewriteRule ^I@I([0-9]+)@.html$ /FamilyTree/legacyIndivid.php?idir=$1 [L]
|
This maps the request to the right destination, but the page does not work properly because all of the relative URLs in the header referring to javascript and CSS files fail, because they are being interpreted relative to the original location in FamilyTree/Web, not the redirected location in Family/Tree. |
|
Back to top |
|
|
|
|
|
|