Welcome to WebmasterWorld Guest from 174.129.135.89

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Advice on Mod Rewrite

Problem rewriting to .php

   
10:45 pm on Oct 12, 2012 (gmt 0)

5+ Year Member



Hey everyone!

I had a question regarding rewrite rules. My site runs all content through index.php and uses GET variables to get the content.

I have some rewrite rules in my .htaccess that are giving me some trouble. A moderator was really helpful in another post and we tidied it up. Currently the rules rewrite to .html, and I would like them to rewrite to .php just for consistency with other pages that are stand alone. However, when I change the .html to .php, I get a "Error 500. Server encountered an error due to..." and the entire site is down. I am not sure what the issue may be. The server is configured for PHP because that is what the site is really running. Here are my current rewrite rules:

RewriteRule ^([^/.]+)\.html$ /index.php?content=$1 [L]
RewriteRule ^([^/]+)/([^/.]+)\.html$ /index.php?content=$1&manufacturer=$2 [L]
RewriteRule ^([^/]+)/([^/.]+)/machine%20shop/\.html$ /index.php?content=$1&service=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/.]+)\.html$ /index.php?content=$1&product=$2&service=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/.]+)\.html$ /index.php?content=$1&MID=$2&category=$3&manufacturer=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/.]+)\.html$ /index.php?content=$1&part=$2&category=$3&MID=$4&manufacturer=$5 [L]


Thanks in advance for any help!

-Joey
11:20 pm on Oct 12, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Currently the rules rewrite to .html

The code does nothing of the sort. A rewrite is an "external URL to internal filepath translation".

What it does is look at incoming requests for .html URLs and rewrite the request pointer to fetch content from a .php file. Presumably the links on your pages point to .html URLs.


and I would like them to rewrite to .php

What do you mean by this?

If you want to change the URLs, you must change the references in the href="" part of the links on the pages of your site. mod_rewrite only processes the request from the browser after the link has been clicked.

If you are gong to change the URLs, then change to extensionless URLs. URLs for pages do not need extensions.

The next step you will need will be to redirect requests for .html URLs to the new extensionless URLs using a RewriteRule placed before all of the existing rewrites.
12:29 am on Oct 13, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Currently the rules rewrite to .html, and I would like them to rewrite to .php just for consistency with other pages that are stand alone. However, when I change the .html to .php, I get a "Error 500. Server encountered an error due to..." and the entire site is down.

:: peering into crystal ball ::

If you look at your Error Logs, you will find a series of messages along the lines of "Server exceeded the maximum number of permitted redirects" due to infinite html-to-php-to-html-to-php loops. I found this useful list [wiki.apache.org] the other day and am posting it here in case I misplace it again. (It doesn't happen to include the infinite-redirects error, but that's clear enough anyway.)

Are your real files php, html or a mixture of both?

A redirect can point to anything you like. It determines what the user sees in their browser's address bar.

A rewrite can only point to a file that really, physically exists.* It determines what the user sees in the body of their browser window.


* Unless, for reasons of your own, you want the rewrite to end in a 404 error.
2:53 am on Oct 13, 2012 (gmt 0)

5+ Year Member



*light bulb*

Ok I see! So to clear it up this is what I am ultimately trying to accompish:

http://www.example.com/home.php

translates to

http://www.example.com/index.php?content=home

In my .htaccess file:

RewriteRule ^([^/.]+)\.php$ /index.php?content=$1 [L]
RewriteRule ^([^/]+)/([^/.]+)\.php$ /index.php?content=$1&manufacturer=$2 [L]
RewriteRule ^([^/]+)/([^/.]+)/machine%20shop/\.php$ /index.php?content=$1&service=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/.]+)\.php$ /index.php?content=$1&product=$2&service=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/.]+)\.php$ /index.php?content=$1&MID=$2&category=$3&manufacturer=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/.]+)\.php$ /index.php?content=$1&part=$2&category=$3&MID=$4&manufacturer=$5 [L]


I get an Error 500 when I use these rewrite rules. The original .htaccess works correctly:

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/.]+)\.html$ /index.php?content=$1&MID=$2&category=$3&manufacturer=$4 [L]
2:54 am on Oct 13, 2012 (gmt 0)

5+ Year Member



Oh and I am using real php files. I will check the logs. Thank you!
7:08 pm on Oct 13, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



After your request has been rewritten to fetch content from
/index.php?content=$1
the new internal pointer again matches the
^([^/.]+)\.php$
pattern in the very first rule and will be rewritten again, then matches again and is rewritten again and again and again.


http://www.example.com/home.php
translates to
http://www.example.com/index.php?content=home

Filenames are used inside the server. Filenames do not have to appear in URLs. For root URL of site never mention the index filename. Canonical URL for site root ends with a trailing slash.

If this is a REWRITE then the wording should be:
"
Requests for http://www.example.com/ are rewritten to fetch content from the internal path /index.php?content=home
"
A rewrite is a URL to file translation so there is NO hostname in the rule target.

If this is a REDIRECT then the wording should be:
"
Requests for http://www.example.com/index.php?content=home are redirected to http://www.example.com/
"
A redirect is a URL to URL translation. The rule target will include the canonical hostname.
8:04 pm on Oct 13, 2012 (gmt 0)

5+ Year Member



Thank you very much for the clarification!

Last question ... I want to remove the problematic text(machine%20shop) in the rules, and when I do there is understandably a conflict since the first and second pattern are the same:

RewriteRule ^([^/]+)/([^/.]+)/$ /index.php?content=$1&manufacturer=$2 [L]
RewriteRule ^([^/]+)/([^/.]+)/$ /index.php?content=$1&service=$2 [L]

This always fetches the content from the first internal path. Is there any way to differentiate these two without adding text to one of them?

Thank you again so much for helping me to get a better understanding of regex and rewrite rules!
8:06 pm on Oct 13, 2012 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Look again at the original code.

The problem with "machine shop" was the extra slash, or the nothing between slash and dot.

The space/%20 is also a concern.
9:04 pm on Oct 13, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Go from most specific to most general. You had it right in the first version:

FIRST
RewriteRule ^([^/]+)/([^/.]+)/machine%20shop\.html$ /index.php?content=$1&service=$2 [L]
AND THEN
RewriteRule ^([^/]+)/([^/]+)/([^/.]+)\.html$ /index.php?content=$1&product=$2&service=$3 [L]

(deleting the / in /\.html which I assume was a typo). Except, shouldn't "machineshop" --suitably punctuated-- be in the query somewhere too?

You can shave a nanosecond off processor time by using [^/.] throughout, but you are already ahead by starting out with [^/]. A lot of people start with .* and we have to go from there :(