SitePoint Sponsor

User Tag List

Results 1 to 22 of 22
  1. #1
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pretty Url Problems

    Hi everyone,

    I'm having some trouble implimenting pretty url's on my site, this code worked before but i changed my sites folder hierarchy, but when i changed the code to match the new structure it broke.

    What i would like is that some one could type in the pretty URL and on the server side it would be converted into the proper URL, as shown below.

    Here is what needs to be done:

    Orginal URL:

    www.site.com/author.php?name=some-authors-name

    Formatted Pretty URL:
    www.site.com/author/some-authors-name

    Here's code:
    Code:
    Options +FollowSymLinks
    RewriteEngine On  
    RewriteCond %{SCRIPT_FILENAME} !-d  
    RewriteCond %{SCRIPT_FILENAME} !-f  
    
    RewriteRule ^author/(.*)$ ./author.php?name=$1
    Any help would be appreciated.

    Thanks,
    Maxdream01
    PHP is FUN

  2. #2
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh sorry did not realize that there is a specific place for this, please move to Server Configuration, Apache & URL Rewriting forum.

    Thanks
    PHP is FUN

  3. #3
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by maxdream01 View Post
    Here's code:
    Code:
    Options +FollowSymLinks
    RewriteEngine On  
    RewriteCond %{SCRIPT_FILENAME} !-d  
    RewriteCond %{SCRIPT_FILENAME} !-f  
    
    RewriteRule ^author/(.*)$ ./author.php?name=$1
    I am 86% certain on this, but I think the issue is with your ./author.php?name=$1

    I think the ./ is telling apache to look in the folder author/ for author.php which doesn't exist. If you change it to the following, it might work
    Code:
    Options +FollowSymLinks
    RewriteEngine On  
    RewriteCond %{SCRIPT_FILENAME} !-d  
    RewriteCond %{SCRIPT_FILENAME} !-f  
    
    RewriteRule ^author/(.*)$ author.php?name=$1
    @dklynn ; would also suggest NOT using (.*), and instead only capture the characters you want. Such as ([a-zA-Z0-9-]+), which captures letters (upper and lowercase), numbers, and the hypen (-).

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

    You must've been "abused" by me as you're doing quite well with your mod_rewrite responses!

    md1,

    I believe that your problem (aside from the obvious (.*))was with the redirection. I must assume that your .htaccess is in your DocumentRoot so the redirection will be relative to that. The problem is that you do not need to specify the current directory with the ./, all you need is author.php.

    Tip: I try to put all my mod_rewrite code in the DocumentRoot's .htaccess so I don't have to consider directory levels (except relative to the DocumentRoot, of course). A second tip (a twofer, how about that!) is to use the R=301 flag (along with the Last flag) so you can see the redirection you've made. At least that would have shown you something really weird and would have forced you to look at the redirection again and you may have noticed the level change.

    WARNING: When you change the directory level, you will create a directory offset for any embedded relative links resulting in the loss of css, js and image links (relative to author.php's location in the DocumentRoot). I'll let you read my signature's tutorial to discover the two options to resolve this problem.

    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
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    cp,

    You must've been "abused" by me as you're doing quite well with your mod_rewrite responses!
    The nightmares were terrible, I finally got over them until a few threads ago No, I really glad that I've run into you in regards to mod_rewrite, I've learned a lot (still have more to learn, but that's why I try).

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

    Whew!

    As for mod_rewrite, all it takes is a little experience (and answering member questions for too many years).

    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 Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey everyone, thanks for the help so far, but its still not working. Instead i'm getting a weird anomaly where my browser is trying to go to www.site.com/author/some-authors-name/index.php.

    here is the code i'm now using, i changed it accordingly:

    Code:
    Options +FollowSymLinks
    RewriteEngine On 
    RewriteCond %{SCRIPT_FILENAME} !-d  
    RewriteCond %{SCRIPT_FILENAME} !-f 
    RewriteRule ^author/([a-zA-Z0-9-]+)$ author.php?name=$1
    PHP is FUN

  8. #8
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Does it work if you change

    Code:
    Options +FollowSymlinks
    to

    Code:
    Options +FollowSymlinks -MultiViews
    ?
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

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

    MultiViews allows a URI to be hijacked if there is an identical filename in the path, i.e., author.php and author/ where author.php will actually be run. I ensure that it's off (-MultiViews) as you can get really weird results if you're not careful.

    You neither showed us your test URI nor the result of an R=301 flag (used for testing, it will display the redirection) but that's about the only thing that could have hijacked your URI UNLESS you have other mod_rewrite (or mod_alias) code in your .htaccess (which should also be displayed for reasonable help).

    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

  10. #10
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    md1,

    MultiViews allows a URI to be hijacked if there is an identical filename in the path, i.e., author.php and author/ where author.php will actually be run. I ensure that it's off (-MultiViews) as you can get really weird results if you're not careful.

    You neither showed us your test URI nor the result of an R=301 flag (used for testing, it will display the redirection) but that's about the only thing that could have hijacked your URI UNLESS you have other mod_rewrite (or mod_alias) code in your .htaccess (which should also be displayed for reasonable help).

    Regards,

    DK
    Thank you! I was playing around with this last night and got weird results using /author/testing becuase I had an author.php, and I eventually found that renaming the file to author2.php worked, and so did using -MultiViews, but I didn't have time to research why it worked yet. Your explanation even helped me

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

    Thanks! I think SitePoint should bring you on staff! Oh, wait, they have You're doing very well so keep learning (and visit apache.org for the straight skinny on their modules and core directives - I do all the time!).

    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

  12. #12
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi everyone, I tried placing -MultiViews in the code and it stopped the the server from trying to go to /author/author-name/index.php, but now I'm receiving a 300 multiple options error, trying to make me go to author.php/author-name/.

    I do not have any other mod_rewrite commands in .htaccess.

    Thanks for the help,
    Maxdream01
    PHP is FUN

  13. #13
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    I had to change the rewrite rule to the following to support hypens and a possible ending slash
    Code:
    Options +FollowSymLinks -MultiViews
    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteRule ^author/([-a-zA-Z0-9]+)/?$ author.php?name=$1

  14. #14
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi cp,

    I tried that code and for some reason i'm only receiving a 404 error.

    Thanks,
    Maxdream01
    PHP is FUN

  15. #15
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by maxdream01 View Post
    Hi cp,

    I tried that code and for some reason i'm only receiving a 404 error.

    Thanks,
    Maxdream01
    What URL were you testing? I did get a 404 for testing /author/author-name/index.php, but it works for /author/author-name/ and /author/author-name

  16. #16
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I recieved a 404 for both /author/author-name and /author/author-name/
    PHP is FUN

  17. #17
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,672
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by maxdream01 View Post
    I recieved a 404 for both /author/author-name and /author/author-name/
    md1,

    There is no reason for those URIs not to work (except for the comment below about trailing /'s). Add the flags I added to cp's code (after confirming that you have author.php in your DocumentRoot (the same directory as this .htaccess).

    Quote Originally Posted by cpradio View Post
    I had to change the rewrite rule to the following to support hypens and a possible ending slash
    Code:
    Options +FollowSymLinks -MultiViews
    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^author/([-a-zA-Z0-9]+)/?$ author.php?name=$1 [R=301,L]
    cp,

    Optional trailing will (almost) always kill relative links in your scripts so, unless you're willing to code absolute links or add the <base> meta tag to the author.php script, it's best not to allow optional /'s! In addition, the {REQUEST_FILENAME} is the variable which is commonly used to test files and directories. I think that {SCRIPT_FILENAME} does give the same answer but I'd have to go look it up and I'm swamped for time right now.

    both,

    The flags can be very useful! In this case, the R=301 flag will tell Apache to display the redirection (to help discover the reason for the 404) and can be removed before the code goes to the production machine. Obviously, the Last flag tells mod_rewrite to restart the next pass through 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

  18. #18
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Dk,

    This change gave me very strange results, if i try to go to /author/author-name/ it tries to take me to /author.php/author-name/index.php, which does not exist, and if i try to type in /author/author-name it tries to take me to the full server root, e.g. http://www.site.com/******/homepages...e=ari-boyarsky, this is the actual server root where the files are located, its a shared server, the * represent information I can't disclose here, but its just a string of numbers that identifies each site on the server.

    Thanks,
    Maxdream01
    PHP is FUN

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

    Now we're getting somewhere!

    Okay, author/author-name/ identifies a directory and DirectoryIndex is obviously set for index.php {others? none were found} so it gave you author/author-name/index.php as it is supposed to do (I trust that you removed the optional trailing / for the reasons above).

    Now, the server root is a problem when /author.php was not requested (when it doesn't find author.php in the server root, it's supposed to look in the domain's DocumentRoot, which it has done, but it's supposed to relative to the DocumentRoot, not the server root). I've had that same problem (on my WinDoze test machine). Many others have had that problem, too, and the only way I/we have found to resolve that problem is to restart Apache. Give that a try as it should restore Apache to full (and correct) functioning.

    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

  20. #20
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    DK,

    I don't believe i have the power to restart apache, firstly the web host the client is using is 1and1, which uses cgi not apache, and even if it did use apache i do not believe i would have the power to restart apache anyway because it is a shared server.

    Thnaks,
    Maxdream01
    PHP is FUN

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

    Other members have asked their hosts to restart and, with the explanation of the erroneous redirection, they've always been happy to do it. If not, then 1&1 is reputed to be a terrible host so it might enable you to find a good host to provide the service you should expect (and must demand).

    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

  22. #22
    SitePoint Zealot maxdream01's Avatar
    Join Date
    Feb 2011
    Location
    USA
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi DK,

    I was actually able to make it work by fiddling with the code a bit:
    Code:
    Options +FollowSymlinks -MultiViews
    RewriteEngine On 
    RewriteCond %{SCRIPT_FILENAME} !-d  
    RewriteCond %{SCRIPT_FILENAME} !-f 
    RewriteRule ^author/([a-zA-Z0-9-]+)(/?)$ /author.php?name=$1
    I'm not entirely sure why it worked this time instead of the times before.

    Also one last question, i know you mentioned before there were 2 ways to fix the issue that would arise with the css, js and other attached files, i know i could change everything to absolute url's but i would rather not do that, i know another option could be to include the files using PHP, so that the style sheet is internal. I this a viable option or is there a better solution.

    EDIT: NM, i figured it out i just used the <base> tag.

    Thanks for all the help everyone,
    Maxdream01
    PHP is FUN


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
  •