Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Posts
    158
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Mod_Rewrite Question

    I'm trying to rewrite some dynamic URLs into SEO friendly URLs and I feel like I'm hitting my head against my desk.

    This works fine:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^([0-9]+)/news/([a-zA-Z0-9_-]+) /news.php?action=view&id=$1 [QSA,NC,L]

    The end result is http://www.domain.com/119/news/title-goes-here

    I'd like to edit this a bit so the identifying ID number comes after the news/ part but so a URL could look like http://www.domain.com/news/119/title-goes-here, however I can't get it to work. Below is what I think is correct:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^news/([0-9]+)/([a-zA-Z0-9_-]+) /news.php?action=view&id=$1 [QSA,NC,L]

    Any ideas why this doesn't work? Is there something with the server settings that could be keeping this from working or is my rules above incorrect?

  • #2
    Regular Coder
    Join Date
    Apr 2011
    Posts
    286
    Thanks
    2
    Thanked 39 Times in 39 Posts
    I don't see anything that sticks out in the rewrite, it might involve something else? Did you try putting something like this at the top of news.php to test it:
    Code:
    print_r($_GET);
    exit;
    To make sure the variables are being passed? Or are you getting 404s?

    By the looks of it, if the bottom rewrite rule doesn't work, the top one shouldn't either.

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by keith1995 View Post
    I'm trying to rewrite some dynamic URLs into SEO friendly URLs and I feel like I'm hitting my head against my desk.

    This works fine:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^([0-9]+)/news/([a-zA-Z0-9_-]+) /news.php?action=view&id=$1 [QSA,NC,L]

    The end result is http://www.domain.com/119/news/title-goes-here

    I'd like to edit this a bit so the identifying ID number comes after the news/ part but so a URL could look like http://www.domain.com/news/119/title-goes-here, however I can't get it to work. Below is what I think is correct:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^news/([0-9]+)/([a-zA-Z0-9_-]+) /news.php?action=view&id=$1 [QSA,NC,L]

    Any ideas why this doesn't work? Is there something with the server settings that could be keeping this from working or is my rules above incorrect?
    if you don't need the title why do you want to catch it, try this:

    RewriteRule ^news/([0-9]+)/.* /news.php?action=view&id=$1 [QSA,NC,L]
    best regards

  • #4
    Regular Coder
    Join Date
    Jul 2004
    Posts
    158
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Wojjie View Post
    I don't see anything that sticks out in the rewrite, it might involve something else? Did you try putting something like this at the top of news.php to test it:
    Code:
    print_r($_GET);
    exit;
    To make sure the variables are being passed? Or are you getting 404s?

    By the looks of it, if the bottom rewrite rule doesn't work, the top one shouldn't either.
    I've tried the print_r and the result is:

    Array ( )

    If I switch the rule back to the one that works, the print_r gives the result of:

    Array ( [action] => view [id] => 117 )

    Why would it work one way and not the other? Could there be a setting with the server?

  • #5
    Regular Coder
    Join Date
    Jul 2004
    Posts
    158
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    if you don't need the title why do you want to catch it, try this:



    best regards
    Thanks for the easier version of the rule. However, this still won't work for me unless I change it to:

    RewriteRule ^([0-9]+)/news/.* /news.php?action=view&id=$1 [QSA,NC,L]

    It makes no sense why it only works if the ID is passed as the first option.

  • #6
    Regular Coder
    Join Date
    Apr 2011
    Posts
    286
    Thanks
    2
    Thanked 39 Times in 39 Posts
    Oh, try using $2 instead of $1? If that works it might be due to the whole match being put in $1, though I don't know why it would do that.

    Or perhaps make it pass $1,$2,$3 to your php script and do the print_r thing again.

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by keith1995 View Post
    Thanks for the easier version of the rule. However, this still won't work for me unless I change it to:

    RewriteRule ^([0-9]+)/news/.* /news.php?action=view&id=$1 [QSA,NC,L]

    It makes no sense why it only works if the ID is passed as the first option.
    try:

    RewriteRule ^(.*)$ /news.php?action=view&id=$1 [QSA,NC,L]

    and print_r($_GET) to see what you get

    best regards

  • #8
    Regular Coder
    Join Date
    Jul 2004
    Posts
    158
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Wojjie View Post
    Oh, try using $2 instead of $1? If that works it might be due to the whole match being put in $1, though I don't know why it would do that.

    Or perhaps make it pass $1,$2,$3 to your php script and do the print_r thing again.
    I can't get it to pass a variable with the rule:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^news/([0-9]+)/.* /news.php?action=view&id=$1

    I've tried substituting $2, $3 and $4 for $1 without any luck.

  • #9
    Regular Coder
    Join Date
    Jul 2004
    Posts
    158
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    try:

    RewriteRule ^(.*)$ /news.php?action=view&id=$1 [QSA,NC,L]

    and print_r($_GET) to see what you get

    best regards
    I've tried this but I get an Internal Server Error and nothing loads.

  • #10
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by keith1995 View Post
    I've tried this but I get an Internal Server Error and nothing loads.
    this is not normal, seems you have something wrong in htaccess. If the other regex work this must work too, maybe not to do what you want but in anycase not a server error.
    can you check for syntax errors, typos or something and try again?

    best regards

  • #11
    Regular Coder
    Join Date
    Jul 2004
    Posts
    158
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    this is not normal, seems you have something wrong in htaccess. If the other regex work this must work too, maybe not to do what you want but in anycase not a server error.
    can you check for syntax errors, typos or something and try again?

    best regards
    I'm at a loss. The .htaccess file looks like this:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^(.*)$ /news.php?action=view&id=$1 [QSA,NC,L]

    This generates the Internal Server Error.

  • #12
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by keith1995 View Post
    I'm at a loss. The .htaccess file looks like this:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^(.*)$ /news.php?action=view&id=$1 [QSA,NC,L]

    This generates the Internal Server Error.
    if you remove the $, give error too? if not what print_r output? and for what url?

    best regards

  • #13
    Regular Coder
    Join Date
    Jul 2004
    Posts
    158
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    if you remove the $, give error too? if not what print_r output? and for what url?

    best regards
    Removed the $ and still get the 500 Internal Server Error.

    If I reach out to the web host about this, what specifically could be the issue? Any ideas?

  • #14
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by keith1995 View Post
    Removed the $ and still get the 500 Internal Server Error.

    If I reach out to the web host about this, what specifically could be the issue? Any ideas?
    i'm sorry, i have no idea.
    about asking support, except describing the problem, how did you try to solve it and what result give you, i have no clue what you can ask.

    best regards

  • #15
    Regular Coder
    Join Date
    Apr 2011
    Posts
    286
    Thanks
    2
    Thanked 39 Times in 39 Posts
    Do you have access to the error log for your site? You can see what the 500 error is for typically in that.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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