Jump to content

Recommended Posts

It seems that my mod_rewrite is working against my PHP script which displays an Article.

 

Here is a stripped down version of my PHP script...

<?php
// Attempt to Retrieve Article.
if (isset($_GET['slug']) && $_GET['slug']) {
	// Slug found in URL.

	// Check Slug Format.
	if (preg_match('~(?x)					# Comments Mode
				^				# Beginning of String Anchor
				(?=.{2,100}$)			# Ensure Length is 2-100 Characters
				[a-z0-9_-]*			# Match only certain Characters
				$				# End of String Anchor
				~i', $_GET['slug'])){
		// Valid Slug Format.

		// Find Article Record.


		// Check # of Records Returned.
		if (mysqli_stmt_num_rows($stmt1)==1){
			// Article was Found.


		}else{
			// Article Not Found.
			// Display Error Page.

		}//End of FIND ARTICLE RECORD

	}else{
		// Invalid Slug Format.
		// Display Error Page.

	}//End of CHECK SLUG FORMAT

}else{
	// Slug Not found in URL.
	// This will never fire!!
	// Apache catches missing slug and re-routes to "articles/index.php"

}//End of ATTEMPT TO RETRIEVE ARTICLE
?>

 

 

Let's say I am reading this Article...

http://local.debbie/articles/postage-meters-can-save-you-money

 

 

Scenario #1

If I delete the slug in the URL, so I have...

http://local.debbie/articles/

 

Then my script takes me to that exact URL because of my mod_rewrite.  (I guess I am okay with that.)

 

 

Scenario #2

If I change the slug to look like this...

local.debbie/articles/postage-meters-can-BOGUS-ENDING

 

Then the "Article Not Found" ELSE branch of the FIND ARTICLE RECORD section fires, and I get an error page, which is what I want.

 

 

Scenario #3

If I change the slug to look like this...

local.debbie/articles/@@@@@

 

Then I would expect the "Invalid Slug Format" ELSE branch of the CHECK SLUG FORMAT section to fire, and display an error page.

 

However, instead, my mod_rewrite steps in and displays...

 

Not Found

 

The requested URL /articles/@@@@@ was not found on this server.

Apache/2.0.59 (Unix) PHP/5.2.6 DAV/2 Server at local.debbie Port 80

 

I don't like that, and it defeats my Regex in my script and bypasses my Error Page.

 

 

---------

In order to get a "Pretty URL", I have this mod_rewrite...

 

RewriteRule articles/([a-zA-Z0-9_-]+)$ articles/article.php?slug=$1

 

 

I guess I understand why the mod_rewrite is stepping in when the URL is changed to...

 

local.debbie/articles/@@@@@

 

...because, after all the Regex in my mod_rewrite isn't coded to handle a bunch of Ampersands?!

 

 

Is there a way to still have a mod_rewrite to create a "Pretty URL", but also let my PHP take care of all the Error-Handling??

 

Thanks,

 

 

Debbie

 

Link to comment
https://forums.phpfreaks.com/topic/263970-issue-with-404-error-and-mod_rewrite/
Share on other sites

Change your rewrite regex to just look for any characters rather than the specific set of characters:

RewriteRule articles/(.+)$ articles/article.php?slug=$1

 

The only issue's that might cause is if you have other files inside the articles/ url that you want to access, but you can bypass them using a RewriteCond to check for their existence first.

 

Change your rewrite regex to just look for any characters rather than the specific set of characters:

RewriteRule articles/(.+)$ articles/article.php?slug=$1

 

Why are you suggesting that?

 

Is the logic that we let my PHP script make sure that a slug is only [a-zA-Z0-9_-] ??

 

 

The only issue's that might cause is if you have other files inside the articles/ url that you want to access, but you can bypass them using a RewriteCond to check for their existence first.

 

I'm not following what you mean?

 

In my "articles" directory, I do have an "add_comment.php" script. 

 

(Honestly, I forgot how all of this works, because I wrote it several months ago, and now I am rewriting things in order to make them more secure and streamlined.)

 

Thanks,

 

 

Debbie

 

Why are you suggesting that?

 

Is the logic that we let my PHP script make sure that a slug is only [a-zA-Z0-9_-] ??

 

Yes, since that is what you asked for:

Is there a way to still have a mod_rewrite to create a "Pretty URL", but also let my PHP take care of all the Error-Handling??

 

The new regex will basically just pass on to your script anything in the url after the articles/ part.  Then within your PHP code you do all your necessary checks and output any appropriate error messages.

 

 

I'm not following what you mean?

 

It means if you had any files such as a CSS file, Image file, JS file, etc that had a url such as local.debbie/articles/styles.css (for example) that it would not be properly handled with the new rewrite rule.

 

If you do not have any cases of url's like that, then you have no issues.

Why are you suggesting that?

 

Is the logic that we let my PHP script make sure that a slug is only [a-zA-Z0-9_-] ??

 

Yes, since that is what you asked for:

Is there a way to still have a mod_rewrite to create a "Pretty URL", but also let my PHP take care of all the Error-Handling??

 

The new regex will basically just pass on to your script anything in the url after the articles/ part.  Then within your PHP code you do all your necessary checks and output any appropriate error messages.

 

I'm waiting for a "gotcha"...    ;)  (Or is this one time I can have it both ways?!)

 

 

I'm not following what you mean?

 

It means if you had any files such as a CSS file, Image file, JS file, etc that had a url such as local.debbie/articles/styles.css (for example) that it would not be properly handled with the new rewrite rule.

 

If you do not have any cases of url's like that, then you have no issues.

 

If I had...

 

local.debbie/articles/styles.css

 

...the that would have caused issues with my original Regex too, right?

 

 

I wish I could remember how my "Add a Comment" script works...

 

In my "article.php" script, I see I have this...

<?php
	// ********************
	// Display Buttons.		*
	// ********************
	if ((isset($_SESSION['loggedIn'])) && ($_SESSION['loggedIn'] == TRUE)){
		// User Logged In.
		// Show button: 'Add a Comment'
		echo '<ul>
				<li>
					<a id="addCommentButton" class="button" href="' . BASE_URL . '/articles/add_comment.php">Add a Comment</a>
				</li>
			</ul>';
	}else{

 

It looks like your new Regex would mess that up, but then again, so would have my old Regex?!  And I know my script worked before...

 

(I'm reviewing "article.php" and "add_comment.php" tonight...)

 

 

Debbie

 

If I had...

 

local.debbie/articles/styles.css

 

...the that would have caused issues with my original Regex too, right?

 

Your original regex did not allow the dot (.) character so it would not have matched such a URL.

 

It looks like your new Regex would mess that up, but then again, so would have my old Regex?!  And I know my script worked before...

Since you would be going to /articles/add_comment.php the new regex probably could cause an issue yes.  As I mentioned, you can use a RewriteCond to test if the file exists and if so, by-pass the rewrite.  I believe for that you would change your rewrite to these lines:

 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule articles/(.+)$ articles/article.php?slug=$1

 

I am not positive on that though.

 

 

Since you would be going to /articles/add_comment.php the new regex probably could cause an issue yes.  As I mentioned, you can use a RewriteCond to test if the file exists and if so, by-pass the rewrite.  I believe for that you would change your rewrite to these lines:

 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule articles/(.+)$ articles/article.php?slug=$1

 

I am not positive on that though.

 

Is there another way to accomplish what I want?

 

Anyone else?

 

 

 

Debbie

 

Since you would be going to /articles/add_comment.php the new regex probably could cause an issue yes.  As I mentioned, you can use a RewriteCond to test if the file exists and if so, by-pass the rewrite.  I believe for that you would change your rewrite to these lines:

 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule articles/(.+)$ articles/article.php?slug=$1

 

I am not positive on that though.

That's right.

 

@doubledee: Any problems? kicken is totally correct with what he's been saying.

Since you would be going to /articles/add_comment.php the new regex probably could cause an issue yes.  As I mentioned, you can use a RewriteCond to test if the file exists and if so, by-pass the rewrite.  I believe for that you would change your rewrite to these lines:

 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule articles/(.+)$ articles/article.php?slug=$1

 

I am not positive on that though.

That's right.

 

@doubledee: Any problems? kicken is totally correct with what he's been saying.

 

It's not working.

 

When I try to go from...

http://local.debbie/index.php

 

To my Article Listing here...

http://local.debbie/articles/index.php

 

It takes me to here which is my error-handling page...

http://local.debbie/account/results.php

 

 

In my Web Root, my .htaccess file was changed to look like this...

RewriteCond %{index.php} !-f
RewriteCond %{add_comment.php} !-f
RewriteRule articles/(.+)$ articles/article.php?slug=$1

##RewriteRule articles/([a-zA-Z0-9_-]+)$ articles/article.php?slug=$1

 

 

Debbie

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.