SitePoint Sponsor

User Tag List

Results 1 to 16 of 16
  1. #1
    SitePoint Enthusiast xor's Avatar
    Join Date
    Jul 2001
    Location
    New Zealand
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mod_rewrite: force a redirect to new URL?

    I have a new version of my website written in PHP and I have used mod_rewrite to capture all the possible URLs from my old site (for those pages in search engines and bookmarked and on postcards sent from the site). But with mod_rewrite the old URL still appears in the address bar of the browser, is there any way to get it to redirect to the new URL so that it changes in the browsers address bar?

    I was looking at mod_alias and the RedirectMatch rules, but for some reason I kept getting errors when I tried to use it. The module seems to be loaded in Apache. And I'd prefer to use mod_rewrite anyway -- now that I've gotten into it I think it's really cool

    Any ideas and help appreciated.

  2. #2
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi :-),

    >> But with mod_rewrite the old URL still appears in the address bar of
    >> the browser, is there any way to get it to redirect to the new URL
    >> so that it changes in the browsers address bar?
    Yes,
    that's the role of the R flag.

    Examples:

    a) internal redirection
    RewriteRule (.+)\.htm^ $1.php

    b) external redirection
    RewriteRule (.+)\.htm^ $1.php [R]

    c) external redirection
    RewriteRule (.+)\.htm^ http://www.example.com/$1.php

    The RedirectMatch directive can make only external redirections.


    Thanks,
    :-) Andrea
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  3. #3
    SitePoint Enthusiast xor's Avatar
    Join Date
    Jul 2001
    Location
    New Zealand
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh cool! easy
    Thanks pippo, I didn't even see that one when I was looking in my Apache Pocket Guide, duh!

  4. #4
    SitePoint Enthusiast xor's Avatar
    Join Date
    Jul 2001
    Location
    New Zealand
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pippo
    ...

    Examples:

    a) internal redirection
    RewriteRule (.+)\.htm^ $1.php

    b) external redirection
    RewriteRule (.+)\.htm^ $1.php [R]

    c) external redirection
    RewriteRule (.+)\.htm^ http://www.example.com/$1.php

    The RedirectMatch directive can make only external redirections.


    Thanks,
    :-) Andrea
    I'm having some problems with this (home now working on it).
    One question first, why the ^ after the \.htm? I had the following:
    Code:
    RewriteRule ^index.cfm$ /index.php [L,NC,R]
    To match exactly the case where only the index.cfm page is called. But that doesn't work, nor does the example b) scenario (I tried many different forms):
    Code:
    RewriteRule index\.cfm^ index.php [R]
    RewriteRule index.cfm^ index.php [R]
    RewriteRule index.cfm^ index.php [R]
    RewriteRule ^index\.cfm$ index.php [R]
    RewriteRule ^index\.cfm$ /index.php [R]
    The first 3 just give a 404 error, so it is not actually redirecting at all. The last 2 give a forbidden error because they are trying to redirect to the following:
    http://max/W:/Websites/Apache2/htdocs/tnzs//index.php

    My site is actually at:
    http://max/tnzs/
    and the request was for
    http://max/tnzs/index.cfm
    which I want to redirect to http://max/tnzs/index.php so I'm really confused as to what is happening here. It's redirecting by appending the file path to the web server root (domain). But I don't understand why. I would like to avoid using the format of example c) because that would cause me to have to change my .htaccess file every time when uploading to my host (since they will have my 'real' domain and my development machine just has it's own local server name.

    I know that \ is the escape character and since . is a special character in the RegExp world it's a good idea to escape it. But if you have the literal value of the filename (eg index.cfm) is it still a good idea to escape the dot? I assume you don't need to escape it when you get to the second parameter since you don't use RegExp there. Is that about right?

    Sorry to be a pain, I am tired after a days work, hope I'm not missing the obvious here.

  5. #5
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    >> One question first, why the ^ after the \.htm?
    >> I had the following:
    Because it was 21:19 and I was sleeping..., so that's a my mistake ..I was tired me too...sorry for that

    It's correct to use:

    a) internal redirection
    RewriteRule (.+)\.htm$ $1.php

    b) external redirection
    RewriteRule (.+)\.htm$ /$1.php [R]

    c) external redirection
    RewriteRule (.+)\.htm$ http://www.example.com/$1.php

    because the $ means end of string, while the ^ means start of string.

    I put in bold what needed to be changed, sorry again.


    Thanks,
    :-) Andrea
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  6. #6
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    >> I know that \ is the escape character and since . is a special
    >> character in the RegExp world it's a good idea to escape it.
    >> But if you have the literal value of the filename (eg index.cfm)
    >> is it still a good idea to escape the dot? I assume you don't
    >> need to escape it when you get to the second parameter
    >> since you don't use RegExp there. Is that about right?
    Yes, in the ``left part'' of rewriterule you have to escape the dot while in the ``right part'' you don't have to.

    Why have I escaped the dot ?

    Because, using your example, ^index.cfm$ will match:
    index.cfm (fine)
    index1cfm (not fine)
    indexAcfm (not fine)
    indexBcfm (not fine)
    ...etc


    :-) Andrea
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  7. #7
    SitePoint Enthusiast xor's Avatar
    Join Date
    Jul 2001
    Location
    New Zealand
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pippo
    >> One question first, why the ^ after the \.htm?
    >> I had the following:
    Because it was 21:19 and I was sleeping..., so that's a my mistake ..I was tired me too...sorry for that
    Thanks,
    :-) Andrea
    hehe, I can sympathize pippo, thanks for clarifying that So that's good, I understood correctly.

    I still get problems with this though, I don't know why...
    Code:
    RewriteRule index\.cfm$ /index.php [R]
    It redirects to this URL:
    http://max/W:/Websites/Apache2/htdocs/tnzs//index.php
    which is wrong of course. But you see what it is doing, going to web server root (http://max/) then appending the actual filepath to the file I want it redirecting to, and I get a 'Forbidden' error. what have I done wrong?

    Same thing without the [R] flag works fine (except doesn't redirect the URL).

    Any ideas what is going wrong?

  8. #8
    SitePoint Enthusiast xor's Avatar
    Join Date
    Jul 2001
    Location
    New Zealand
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pippo
    Why have I escaped the dot ?

    Because, using your example, ^index.cfm$ will match:
    index.cfm (fine)
    index1cfm (not fine)
    indexAcfm (not fine)
    indexBcfm (not fine)
    ...etc

    :-) Andrea
    Oh, ok gotcha! Yes it makes sense when I see your example. The '.' could be any character except newline. So I will go back and make sure my rewrite rules all have \. on the left side.

    Thank you! You are a great teacher, as well as a RewriteRule guru!

  9. #9
    SitePoint Enthusiast xor's Avatar
    Join Date
    Jul 2001
    Location
    New Zealand
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So I still get problems with this though, I don't know why...
    Code:
    RewriteRule index\.cfm$ /index.php [R]
    It redirects to this URL:
    http://max/W:/Websites/Apache2/htdocs/tnzs//index.php
    which is wrong of course. But you see what it is doing, going to web server root (http://max/) then appending the actual filepath to the file I want it redirecting to, and I get a 'Forbidden' error. what have I done wrong?

    Same thing without the [R] flag works fine (except doesn't redirect the URL, which is what I want).

  10. #10
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's strange because it should add the server name correctly,
    ( on my pc it works as expected, I simulated your scenario )

    the shortest answer I can suggest is:

    RewriteRule index\.cfm$ http://max/index.php [R]


    but please also try to do this and let me know:

    RewriteEngine On
    RewriteBase /
    RewriteRule index\.cfm$ /index.php [R]

    maybe you are using some kinds of aliases so using the rewritebase you should be okay.



    Thanks,
    :-) Andrea
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  11. #11
    SitePoint Enthusiast xor's Avatar
    Join Date
    Jul 2001
    Location
    New Zealand
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pippo
    That's strange because it should add the server name correctly,
    ( on my pc it works as expected, I simulated your scenario )

    the shortest answer I can suggest is:

    RewriteRule index\.cfm$ http://max/index.php [R]


    but please also try to do this and let me know:

    RewriteEngine On
    RewriteBase /
    RewriteRule index\.cfm$ /index.php [R]

    maybe you are using some kinds of aliases so using the rewritebase you should be okay.



    Thanks,
    :-) Andrea

    I tried that, still get the same thing happening. I thought I would upload the .htacces file to my host and try it there as well to see what happened. That was interesting, I got the right effect, but it seemed to be picking up the wrong url. I have a domain name, but because this new site is not live yet, I've got it as a subdomain under my host at the moment. It didn't pick up the subdomain, but the real domain. So I don't know how, my host my have that set up already in preparation for the change over. So maybe I am using some funny aliases on my development machine in the apache httpd.conf file. I will do a search through it for 'alias' and anything else you can thing of to search for?

  12. #12
    SitePoint Enthusiast xor's Avatar
    Join Date
    Jul 2001
    Location
    New Zealand
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just tried modifying your suggestion a bit to...
    Code:
    RewriteBase /tnzs
    (I've got this site in a folder called /tnzs and so my local url is http://max/tnzs/)
    This works! But only if I do not use the [R] flag Don't know what's happening there then. So...

    Code:
    RewriteBase /tnzs
    works on my local machine, but only if I don't use [R] flag as well.

    Code:
    RewriteBase /
    works on my host only if I do use [R] flag, but redirects to my domain name, so this will not be a problem as soon as I go live with this site.

    I'd like to not have different .htaccess files for my development and live sites, so if you can think of why it has a problem with the [R] flag on my local machine I can do whatever I want in the apache httpd.conf file -- if that's what it takes.

  13. #13
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay,
    could you please attach here a copy of your httpd.conf ?
    Will tnzs be the place where you will want to develop a new web site (ie a new domain name ? ) ?
    If so I could change your httpd.conf so that you won't have to use a subdir anymore :-).


    Thanks,
    :-) Andrea
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  14. #14
    SitePoint Enthusiast xor's Avatar
    Join Date
    Jul 2001
    Location
    New Zealand
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Andrea!

    Yes, tnzs is the folder in which I have this new website. So my file structure looks like this...

    Apache2\htdocs\tnzs
    Apache2\htdocs\dev
    Apache2\htdocs\phpMyAdmin

    etc...

    And each of those folders represents a different website. Since this is just on my local development machine, it has been ok so far to just use the http://servername/dirName/ convention. I'm just running up to some problems with that now in my development work.

    I guess this is the virtual host stuff? Can you recommend a good book or online resource for Apache, esp the directory and virtual host stuff? I have read the Apache documentation, but I'm still a bit confused by it. I think it's a matter of the terminology they use. I'm sure it's fairly simple once you understand it, but I just need to get to that point

    I've attached my httpd.conf file, but I had to change the extension because .conf isn't recognized. I didn't want to paste the whole thing into this thread, because it's so long.
    Attached Files Attached Files

  15. #15
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay,
    I took the freedom to modify your httpd.conf and enable virtual hosts inside it :-).
    I attach the httpd.conf edited,
    all modifcations I did was commented with the word pippo ( do a search in that file for pippo ).

    Also you need to modify your hosts file too,
    this file is located at
    %WINDIR%/system32/drivers/etc/host

    inside it just edit this line as:

    127.0.0.1 localhost tnzs.localhost

    the bold part is what you need to add.

    So basically now when you type
    tnzs.localhost
    you will have the website for tnzs

    if you type localhost you will have the default development enviroment.

    One note,
    I saw that you used Apache 2.
    Well If I were you I'd play with 1.3.
    Why ? Because your server enviroment is probably running 1.3 and so it's better to have the same version of both development and serrver enviroments.


    I think that if you see what I did about virtual hosts and with the help of the documentation (official) and asking here for any doubts...you should be ok :-).

    Try to check my resource collection too, maybe some interesting links could be there too...

    Let me know how it worked,
    :-) Andrea
    Attached Files Attached Files
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  16. #16
    SitePoint Enthusiast xor's Avatar
    Join Date
    Jul 2001
    Location
    New Zealand
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much pippo!

    You are right, my host uses Apache 1.3.x and I am now trying to match their set up as much as possible to avoid any problems.

    It may take me a couple of days to try this out and post back here the results. Unfortunately my site (on my development server) is having major problems all of a sudden -- I don't know why. The problems are all with the function GetImageSize() which has been fine, now it is having a problem with the file streams. I have read that this is a known bug. I installed the latest php last night, it's meant to fix the problem, but it still persists I downloaded all my files that were on my host (which work fine) and they have the same problem on my development machine. I don't know what I could have done or changed to cause this problem.

    So I need to get that sorted out first, so that my site works again, as soon as I do I will try your changes and let you know the result. I had a look anyway, that's great how you commented them, so now I can see what you did and learn a bit too. Thank you!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •