SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Need help writing from asp to php to pretty urls

    Hi,

    I have an asp site on a windows server and I am currently rewriting it to php then will be putting it on a linux server, where I am testing everything!

    I am using the following to rewrite to the .asp extension - the site is over 16 years old and has thousands of links scattered around the net with the .asp extension so I don't want to loose all that link juice.

    Code:
    RewriteRule ^(([^/]+/)*[^.]+)\.asp$ /$1.php [R]
    I am writing all of my php pages with the same name as the asp pages and the above works so I am happy that I got that to work

    I am now trying to get a pretty URL and have used the following and it works

    Code:
    RewriteRule  ^navigation/map$ /nav/nzmap.php
    Now what I have not been able to work out is this senario.

    Someone clicks on a link to nzmap.asp which from rule 1 will call the nzmap.php page (works)
    THEN the nzmap.php page will show the URL navigation/map as of rule 2 (that does not happen.)

    I can not get the nzmap.php page to automatically update to navigation/map in the browser?

    I did play around with different positions and managed to create an eternal loop!

    Any help or guidance will be most appreciated

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,605
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    emotn,

    Wrong direction! mod_rewrite is used to convert from a non-serveable URI to one Apache can serve (navigation/map => nav/nzmap.php can work but that requires a lot of intelligence and, other than using regex, mod_rewrite is a dumb language.

    Have a read of the tutorial linked in my signature for other help.

    BTW, kudos for understanding that changing from .asp to .php is trivial. Continue that line of reasoning and you'll be okay.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post

    Wrong direction! mod_rewrite is used to convert from a non-serveable URI to one Apache can serve (navigation/map => nav/nzmap.php can work but that requires a lot of intelligence and, other than using regex, mod_rewrite is a dumb language.

    DK
    Wow! That so makes sense and now I understand the purpose of of mod-rewrite - if I don't have a physical .asp file then I can not rewrite that to a pretty URL.

    Your tutorial also is very helpful - I found a few things in there that I was thinking of looking for after I finished the main re-coding.

    Thank you for your time - it is appreciated

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,605
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    emotn,

    No problem! I'm glad that the tutorial has helped another SitePoint member.

    I'm a bit concerned over your no .asp, no redirection as that's just not true! First, mod_rewrite only matches the pattern in the regex of the URI then effects a redirection as specified. The power of regular expressions means that you can build complex patterns to exactly match specific URIs. In your case, redirecting from a X.asp URI to X.php is trivial (you nailed that one). There is nothing in your code that says that either X.asp file or X.php file has to exist.

    If you need to ensure that either is a file which exists, you can use RewriteCond %{REQUEST_FILENAME} -f or modify %{FILENAME} to provide the path from the physical root to the file, i.e., %{DOCUMENT_ROOT}X.asp or %{DOCUMENT_ROOT}.php - but that's another subject.

    If you want to redirect to a "pretty URI," do it before redirecting to the .php version, i.e., X.asp => X => X.php. In the first redirection, make the redirection with the R=301 flag then Chain to X => X.php with only the Last flag. Problem (redirect from ASP scripts to a pretty URI then serve the PHP script) resolved! It's not magic (other than the regex), it's merely thinking about the problem logically then generating the code.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question trying to chain ...

    Once again thank you for your feedback

    I have now tired the following
    Code:
    RewriteRule ^(([^/]+/)*[^.]+)\.asp$ /navigation/map$ [C,R=301] 
    RewriteRule  ^nav/nzmap.php  /navigation/map$[L]
    and if I type in nav/nzmap.asp it goes to nav/nzmap.php but what it won't do is change in the browser to /navigation/map

    If I type /navigation/map into the browser I get was not found on this server.

    I have also tried the second line the other way around but that errors.

    I know that I have to use regex but I want to get one to work so I can figure out the positioning of where each one needs to go before I start getting fancy!

    If you could kindly help me to get it to work, that would be so appreciated, or point me in the right direction

    Once again, thank you for your time!

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,605
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    emotn,

    Okay, I misunderstood your "specification" so I need a reset.

    First, my apology for using the "chain" term which is defined as
    Quote Originally Posted by apache.org
    chain|C Rule is chained to the following rule. If the rule fails, the rule(s) chained to it will be skipped.
    I had used it intentionally but without realizing that you COULD have the intermediate URI as a request. That was a silly mistake on my part because, of course, the intermediate URI can't be served!

    Then, I thought you'd wanted the intermediate URI displayed. Didn't you want that as the "pretty URI?"

    Finally, you're hardcoding the link in the mod_rewrite and not establishing a pattern to capture and redirect to the intermediate URI and thence to the PHP script.

    I'm just not sure why you'd want to capture the path and ASP script name (without the extension) and redirect that to ... navigation/map (the $ is an error).
    Code:
    RewriteRule ^(([^/]+/)*[^.]+)\.asp$ /navigation/map$ [C,R=301] 
    RewriteRule  ^nav/nzmap.php  /navigation/map$[L]
    IMHO, this should be (if my "specification' were correct):
    Code:
    RewriteEngine on
    # redirect to "pretty URI"
    RewriteRule ^(([^/]+/)*[^.]+)\.asp$ $1 [R=301]
    # the .asp has been removed and the new URI displayed
    
    # redirect to PHP version of the script
    RewriteRule ^(([^/]+/)*[^.]+)$ $1.php [L]
    In other words, if you have the power of regular expressions, use it!
    Quote Originally Posted by emotn
    ... if I type in nav/nzmap.asp it goes to nav/nzmap.php but what it won't do is change in the browser to /navigation/map

    If I type /navigation/map into the browser I get was not found on this server.

    That's my error for using "chain."

    I have also tried the second line the other way around but that errors.

    Correct, it shouldn't work.
    Of course, if my interpretation of your "specification" is incorrect, then you'll have to rewrite it precisely for better help. One word of warning, though, is that carrying through the same path and filename (sans extension) is HIGHLY advisable as, to do otherwise, would require massive amounts of code and/or a RewriteMap (which is not available to you unless you own the Apache daemon).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Post

    at least you know I do read what you say and try and do as you suggest even if I have to search for stuff - like chain!

    Finally, you're hardcoding the link in the mod_rewrite and not establishing a pattern to capture and redirect to the intermediate URI and thence to the PHP script.
    Yes, I did that so I could follow what was happening before I put it into a whole lot of symbols and not get confused! Just using symbols would have confused me and I would not have understood what part is what! Hardcoding helps me to understand what goes where initially.

    This is what I am trying to acheive:

    Someone clicks on a link somewhere on the net - say mysite.com/navmap.asp?ID=1&This=SomethingElse

    now because I am moving to linux I have rewritten all the asp pages into php with the same asp name so the following takes care of getting a page to display

    Code:
    RewriteRule ^(([^/]+/)*[^.]+)\.asp$ /$1.php [R]
    I'm just not sure why you'd want to capture the path and ASP script name (without the extension) and redirect that to ... navigation/map
    Now this is the next part - I then thought well seeing as the pages are php and on linux I could then get the URI to become
    mysite.com/navigation/map
    - and that would be what would be in the browser.

    I could then get rid of mysite.com/navmap.asp?ID=1&This=SomethingElse OR mysite.com/navmap.php?ID=1&This=SomethingElse

    I hope that explains it more clearly what I am hoping to achieve - if is not doable no worries - I have the .asp links still working and that is the main thing! Just thought I could get a bit fancy

    I really appreciate all of your time - thank you once again.

  8. #8
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,605
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    emotn,

    Ha! I figured that the use of the hardcoded mod_rewrite was an attempt to test the specific code. However, my point is that the pattern is critical to the redirection.

    Your first redirection (.asp => .php) you've done perfectly! Okay, I'd probably have built-in a RewriteCond to test that the .php version exists (as a file). We can get back to that later (or rely on an ErrorDocument 404 /404.php directive).

    Your second redirection (.php to {link-you-cannot-serve}) is another problem. Unfortunatly, my attempt with the above code merely retained the extensionless path/file as the INTERMEDIATE step then on to the .php (serveable URI) as the final step.

    The problem comes when you changed from retaining the path/filename to something different. That causes a major problem in creating a pattern for regex to operate on which means either a long series of directives (bad on a shared host) OR using a RewriteMap. At this stage, I believe you need to retain the path/filename to get comfortable with mod_rewrite.

    On to the query string. That actually offers a possibility for your alternative to path/filename - if it contains useful information which you need to carry forward to your .php scripts. ID=1 and This=SomethingElse just leaves me cold so this may not be possible.

    Okay, perhaps I've gotten a bit ahead of you with this "pretty URI" thing but, if you look at the links to the MANY pages at http://wilderness-wally.com, you'll see that I've set it up for my client to use a page title as the URI - the mod_rewrite code redirects to the handler and accesses the database for the title of the requested page. It only requires changing spaces to _'s (and back), making the title field unique in the database and limiting the characters allowed in the title to those allowed in a URI. If you're interested, please ask, otherwise I'll take your happiness with .asp => .php as satisfaction with this thread.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  9. #9
    SitePoint Member
    Join Date
    Apr 2012
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DK,

    I like what you have done to wilderness-wally, but I know at this point in time I would not be able to convert the original asp scripts as most of the query strings are numbers

    But, I do have sections that are already in php so those I may be able to code up then change the URIs through the mod-rewrite.

    But that is another topic for another day if I can not figure that one out myself

    Once again thank you soo much for all of your time - it is appreciated!

  10. #10
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,605
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    emotn,

    That's the beauty of NOT using the numbers in the links! Access the record using UNIQUE titles and you'll have the "pretty URIs" you're after. Okay, at this point, that would require two more mod_rewrite statements (one to confirm something.php exists as a file for the extensionless redirection and one for the title to handler file directive), completely re-doing your links (using a modified title field then converting back to access the database via the title rather than record id) and an auto-prepend-file() (to permanently redirect once the OLD link's been established and the title field obtained) but wasn't that a nice treatment?

    No worries!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Tags for this Thread

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
  •