...

View Full Version : Mod_Rewrite Question



keith1995
04-25-2011, 05:23 PM
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?

Wojjie
04-25-2011, 05:53 PM
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:

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.

oesxyl
04-25-2011, 08:37 PM
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

keith1995
04-26-2011, 06:19 AM
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:

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?

keith1995
04-26-2011, 06:21 AM
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.

Wojjie
04-26-2011, 06:43 AM
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.

oesxyl
04-26-2011, 09:34 AM
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

keith1995
04-26-2011, 03:50 PM
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.

keith1995
04-26-2011, 03:51 PM
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.

oesxyl
04-26-2011, 05:47 PM
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

keith1995
04-26-2011, 05:59 PM
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.

oesxyl
04-26-2011, 06:03 PM
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

keith1995
04-26-2011, 06:13 PM
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?

oesxyl
04-26-2011, 06:25 PM
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

Wojjie
04-26-2011, 09:58 PM
Do you have access to the error log for your site? You can see what the 500 error is for typically in that.

keith1995
04-27-2011, 04:16 PM
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

The hosting company doesn't offer .htaccess support but they think the syntax is wrong. I don't see how that can be because it works fine one way but when I move the URL properties around slightly, it stops passing the variable.

keith1995
04-27-2011, 04:17 PM
Do you have access to the error log for your site? You can see what the 500 error is for typically in that.

From what the hosting company has said, the 500 Internal Server Error was due to an endless redirect being created with the .htaccess file. I'm so confused...

oesxyl
04-27-2011, 06:31 PM
From what the hosting company has said, the 500 Internal Server Error was due to an endless redirect being created with the .htaccess file. I'm so confused...
it's a guess but i think they already use rewrite in the main server configuration, for hosting purpouse, and probably this create problems( subsequent requests).

best regards

keith1995
05-01-2011, 04:00 AM
Okay, I think we've finally figured out this issue. The server which the site is hosted on is setup to load a page without a file extension, so by using the RewriteRule Rule of ^news/([0-9]+)/.* /news.php?action=view&id=$1, it is loading http://www.domain.com/news and automatically redirecting to http://www.domain.com/news.php which lists all of the news items rather then deliver the user directly to the news story.

So, my next question is how do I force the file extension to be required?

oesxyl
05-01-2011, 05:45 PM
Okay, I think we've finally figured out this issue. The server which the site is hosted on is setup to load a page without a file extension, so by using the RewriteRule Rule of ^news/([0-9]+)/.* /news.php?action=view&id=$1, it is loading http://www.domain.com/news and automatically redirecting to http://www.domain.com/news.php which lists all of the news items rather then deliver the user directly to the news story.

So, my next question is how do I force the file extension to be required?
if they use apache mod-action to do this maybe you can use same way to put them back:

http://httpd.apache.org/docs/2.2/mod/mod_actions.html

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum