Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule Exception

How do I add an exception to the RewriteRule so that it does not redirect.

         

chadhaajay

11:11 am on Aug 6, 2010 (gmt 0)

10+ Year Member



Hi!

My .htacces content is mentioned below

Options +FollowSymLinks All -Indexes
RewriteEngine on
RewriteRule page-(.*)\.html(.*)$ page.php?ID=$1

At present it works fine in the sense that all page.xx files are redirected to page.php?ID=xx

Now, I'd like to add an exception to this RewriteRule so that it does not redirects when xx is 113 because I already have the page-113.html in that folder. So, I'd like to add the page-113.html as exception. There are 2 more pages (page-114 and page-119.html)

What change do I need to make in my .htaccess code so that it does not redirect in the case of 113, 114 and 119 pages?

Thanks in advance!

g1smd

11:38 am on Aug 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Firstly, that code should not redirect. It's an internal rewrite. Please clarify.

Add the
[L]
flag to that rule.

The first
(.*)
pattern is very inefficient. It might be better coded as
([0-9]+)
so that it matches only digits in the request.

The
(.*)
pattern after
\.html
is not needed. Remove it.

Add a negative match RewriteCond using the ! operator and looking at
REQUEST_URI
for your exclusions. The | OR operator can be used like this
11(3|4|9)
to good effect here.

chadhaajay

11:51 am on Aug 6, 2010 (gmt 0)

10+ Year Member



Hello g1smd,

I've changed the code as advised by you. It now looks like

Options +FollowSymLinks All -Indexes
RewriteEngine on
RewriteRule page-([0-9]+)\.html$ page.php?ID=$1

But how do I add exception for page-113.html? I tried

Options +FollowSymLinks All -Indexes
RewriteEngine on
RewriteRule page-([0-9]+)\.html$ page.php?ID=$1 !^page-113.html [L]

but it throws HTTP 500 error.

jdMorgan

3:54 pm on Aug 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need a negative-match RewriteCond, as previously mentioned:

Options +FollowSymLinks All -Indexes
RewriteEngine on
#
RewriteCond $1 !=113
RewriteRule ^page-([0-9]+)\.html$ page.php?ID=$1 [L]

Jim

chadhaajay

4:43 pm on Aug 6, 2010 (gmt 0)

10+ Year Member



It still does not work. I get the following error

Not Found

The requested URL /folder/page-113.html was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

jdMorgan

5:50 pm on Aug 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So, you have custom error documents declared, but they do not exist. That is one problem, and it should be addressed first. Either remove the ErrorDocument directives, correct them to point to existing files (not URLs) or --if you have no ErrorDocument directives in .htaccess files that you control-- look in your control panel and turn off or correct the custom error documents.

The code I posted does not rewrite to "/folder" so I cannot diagnose your problem without more information:

Where did you put this code? -- in what directory's .htaccess file is it located?

Do you have any other rules that rewrite to "/folder"? (If so, then your rule order is likely incorrect, or the RewriteRule or RewriteCond pattern of the other rule is not specific enough.

Rule order is important, as is the use of the [L] flag on all rules, unless you know a very good reason why you don't want to use an [L] flag.

Jim

chadhaajay

4:18 am on Aug 7, 2010 (gmt 0)

10+ Year Member



Hi jdMorgan,

The .htaccess file is present in the directory "folder" and there is nothing in it except these lines.

Options +FollowSymLinks All -Indexes
RewriteEngine on
RewriteRule page-(.*)\.html(.*)$ page.php?ID=$1

At present what I've done to achieve the target is rename the page-113.html to view-113.html and add another line in the .htaccess code below

Options +FollowSymLinks All -Indexes
RewriteEngine on
RewriteRule page-113\.html$ view-113.html
RewriteRule page-(.*)\.html$ page.php?ID=$1

The above code works for me however I do not find it optimized because I'll need to add a new line for every static page that I'd like to open without passing to page.php.

g1smd

9:08 am on Aug 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If the .htaccess file is located in /folder/ then you'll need to use a URL request with /folder/ in it for the rule to work.

If that's incorrect, then instead the rewrite rule should be located in the root .htaccess file of the site.

Do add the [L] flag to the end of every rule.

chadhaajay

11:05 am on Aug 7, 2010 (gmt 0)

10+ Year Member



Hi g1smd,

The .htaccess file is present in the "/folder/" and the URL request is [sitename.com...]

What does the [L} flag means?

phranque

12:39 pm on Aug 7, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



the RewriteRule [httpd.apache.org] L flag indicates last rule which means:
Stop the rewriting process here and don't apply any more rewrite rules.

jdMorgan

2:28 pm on Aug 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, there is something strange about your URL-paths, then. Because of this, it must be the start-anchoring that is interfering with the rule match. As a test, remove the start anchor:

Options +FollowSymLinks All -Indexes
RewriteEngine on
#
RewriteCond $1 !=113
RewriteRule page-([0-9]+)\.html$ page.php?ID=$1 [L]

If that makes the rule "work," then you need to find out what the actual requested URL-path is, and modify the RewriteRule pattern to match it. Because this code is located in /folder/.htaccess, the requested URL-path should be stripped of the "folder/" path-part, but there must be something there that causes the requested URL-path to NOT start with "page-" -- There must be something ahead of "page-" to make my original anchored rule fail to match.

Look also for internal-rewrite rules in the main .htaccess file in the root diretory. Be very sure that no internal rewrites in that file can rewrite the URLs that should be handled by this rule in /folder/.htaccess. If such rules are present, I suggest that you re-arrange things -- perhaps moving this rule up into the main .htaccess file so that it is executed first before the other internal rewrites that might affect these requests.

Jim