Forum Moderators: phranque

Message Too Old, No Replies

Removing Extension Causing Problems

         

berrywoods

11:41 pm on Jul 9, 2010 (gmt 0)

10+ Year Member



Hi Guys, right, ive got this code below, which at the moment, strips off .php from files that excist and sends them to actual php file. On top end everything works fine, None of the top end files conatain any real data and everything is based on includes from directories. (Oscommerece setup if anyones familar)

Had problems with basket not updating with changed urls but seemed to have solved that. Now when im going through checkout when selecting delivery method it doesnt continue and appears to do nothing.

I dont really want to mass remove all extensions from the actual files and then use force to php.

Now one question? im assuming my script (which is bits of managed to dig up) is affecting any and all php files regardless of how many directories its in.

Ill paste the entire htaccess file just incase something else is the cause, the bit we're looking at is the last 2 sections

RewriteEngine on
php_flag register_globals on
Options +FollowSymLinks

RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.example.co.uk/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^example.co.uk
RewriteRule (.*) http://www.example.co.uk [R=301,L]

RewriteCond %{HTTP_HOST} ^www.forum.example.co.uk
RewriteRule (.*) http://www.example.co.uk/forum [R=301,L]

RewriteCond %{HTTP_HOST} ^forum.example.co.uk
RewriteRule (.*) http://www.example.co.uk/forum [R=301,L]

RewriteBase /

RewriteRule ^(.*)-p-(.*)$ product_info?products_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-c-(.*)$ index?cPath=$2&%{QUERY_STRING}
RewriteRule ^(.*)-m-(.*)$ index?manufacturers_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pi-(.*)$ popup_image?pID=$2&%{QUERY_STRING}
RewriteRule ^(.*)-by-(.*)$ all-products?fl=$2&%{QUERY_STRING}
RewriteRule ^(.*)-t-(.*)$ articles?tPath=$2&%{QUERY_STRING}
RewriteRule ^(.*)-a-(.*)$ article_info?articles_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-au-(.*)$ articles?authors_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pr-(.*)$ product_reviews?products_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pri-(.*)$ product_reviews_info?products_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-f-(.*)$ faqdesk_info?faqdesk_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-fc-(.*)$ faqdesk_index?faqPath=$2&%{QUERY_STRING}
RewriteRule ^(.*)-fri-(.*)$ faqdesk_reviews_info?faqdesk_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-fra-(.*)$ faqdesk_reviews_article?faqdesk_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-i-(.*)$ information?info_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-links-(.*)$ links?lPath=$2&%{QUERY_STRING}
RewriteRule ^(.*)-pm-([0-9]+)$ info_pages?pages_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-n-(.*)$ newsdesk_info?newsdesk_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-nc-(.*)$ newsdesk_index?newsPath=$2&%{QUERY_STRING}
RewriteRule ^(.*)-nri-(.*)$ newsdesk_reviews_info?newsdesk_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-nra-(.*)$ newsdesk_reviews_article?newsdesk_id=$2&%{QUERY_STRING}
RewriteRule ^(.*)-po-([0-9]+)$ pollbooth?pollid=$2&%{QUERY_STRING}


# Strip Extension and Generate Rule
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php



# Redirect to New URL Generated Above
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/\ ]+/)*[^.\ ]+\.php\ HTTP/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.example.co.uk/$1 [R=301,L]





berrywoods

12:19 am on Jul 10, 2010 (gmt 0)

10+ Year Member



Still been fiddeling cannot find a solusion, i guess the only way i can see it working is maybe, rule it so only the top level directory is affected, any requests beyond top levels without an extension (minus admin directory) is rewritten with the .php back on so language files load correctly. and then i shall have to manually put back redirects to https as changes in name have affected that too...

jdMorgan

1:13 am on Jul 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try qualifying your last rule by adding:

RewriteCond $1 !admin/
RewriteCond %{SERVER_PORT} =80

ahead of the other two rewriteconds.

For each of your "products"-type URLs modify the patterns to use a more-specific and more-efficient pattern, use the [QSA] flag instead of manually re-appending %{QUERY_STRING} and add an [L] flag. Example, change

RewriteRule ^(.*)-p-(.*)$ product_info?products_id=$2&%{QUERY_STRING}

to

RewriteRule ^[^\-]+-p-(.+)$ product_info?products_id=$1 [QSA,L]

You may notice a significant speed-up of your site from doing this.

There are several other problems, but the above are the most significant.

Jim

berrywoods

8:44 am on Jul 10, 2010 (gmt 0)

10+ Year Member



Using that entire new rule causing the rewrite not to find the page, or products not to be found,

amending too

RewriteRule ^(.*)-p-(.*)$ product_info.php?products_id=$2&% [QSA,L]

did however work and it seems a little quicker.

The Standard rules did come with Ultimate SEO for oscommerece.

Anyone have any ideas on removing the extension?

g1smd

11:06 am on Jul 10, 2010 (gmt 0)

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



The (.*) patterns are VERY inefficient and should be changed to a more specific pattern.

berrywoods

12:36 pm on Jul 10, 2010 (gmt 0)

10+ Year Member



ok, i know the Ultimate SEO pack for oscommerece is quite old now as is the software itself,

still fiddling with this extensionless,

so far. Htaccess redirects all extensionless requests to its php file, apart from any of the checkout files causing problems when done as above, which are actually extensionless as are its language files.

Checkout loads up and continues fine, if the rule says all URLs have preceeding slash apart from checkout URLs, once i add the rule in for all URLs to have slashes at end the php include for the language file assumes its a directory and nto a file and then fails to find anything, even when its redirected a slashed version to a non slashed version.

If i cant find a cure ill have to have slashes on the end of everything except checkout pages.

jdMorgan

6:57 pm on Jul 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps it's a "greediness" issue, as would occur if the string preceding "-p-" itself contained hyphens. The following mod would address that, while still providing a significant performance improvement over ".*":

RewriteRule ^([^\-]+-)+p-(.+)$ product_info?products_id=$2 [QSA,L]

Also, put all of your external redirects ahead of any of your internal rewrites. You last rule should be moved above your current second rule.

Jim

[edited by: jdMorgan at 9:45 pm (utc) on Jul 11, 2010]

berrywoods

10:59 am on Jul 11, 2010 (gmt 0)

10+ Year Member



Thanks JD, im afraid your replacement script is still causing the products pages to show blank,

I have fixed my extension problem, anyone who happends to be searching on google for oscommerece no extensions or no PHP extension can use this as they will.

The idea is that having a preceeding slash after checkout_shipping or any other checkout pages causes problems, to get round this, change all checkout pages including their language files to extensionless files. and force parse them as php

sorry in advance if any of the below code is wrong, or just silly ways of doing things, im still new to htaccess and most of this has been trial and error.

Below code will replace all URL's With extensionless URL's with preceeding / except for checkout pages which show without the slash, and still work. Also code in their to direct users to landing page if selecting index.php or when missing the www.

#Force Parse PHP Extensionless Files

<Files "checkout_shipping">
ForceType application/x-httpd-php
</Files>

<Files "checkout_payment">
ForceType application/x-httpd-php
</Files>

<Files "checkout_confirmation">
ForceType application/x-httpd-php
</Files>

<Files "checkout_success">
ForceType application/x-httpd-php
</Files>

<Files "checkout_shipping_address">
ForceType application/x-httpd-php
</Files>

<Files "checkout_payment_address">
ForceType application/x-httpd-php
</Files>

<Files "checkout_process">
ForceType application/x-httpd-php
</Files>

#Enable Engine and Allow Globals

RewriteEngine on
php_flag register_globals on
Options +FollowSymLinks

#Direct to landing page if index.php is requested

Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.example.co.uk/$1 [R=301,L]

#Direct to landing page if no www

RewriteCond %{HTTP_HOST} ^example.co.uk
RewriteRule (.*) http://www.example.co.uk [R=301,L]

#redirect to slash if no slash
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteCond %{REQUEST_URI} !checkout*
RewriteRule (.*)$ http://www.example.co.uk/$1/ [R=301,L]


# Strip Extension and Generate Rule with a slash at end
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/$ $1.php


# Redirect to New URL Generated Above with slash if requested a .php
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/\ ]+/)*[^.\ ]+\.php\ HTTP/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.berrywoods.co.uk/$1/ [R=301,L]


#Ultimate SEO START
RewriteBase /

RewriteRule ^(.*)-p-(.*)$ product_info?products_id=$2&% [QSA,L]
RewriteRule ^(.*)-c-(.*)$ index.php?cPath=$2&% [QSA,L]
RewriteRule ^(.*)-m-(.*)$ index.php?manufacturers_id=$2&% [QSA,L]
RewriteRule ^(.*)-pi-(.*)$ popup_image.php?pID=$2&% [QSA,L]
RewriteRule ^(.*)-by-(.*)$ all-products.php?fl=$2&% [QSA,L]
RewriteRule ^(.*)-t-(.*)$ articles.php?tPath=$2&% [QSA,L]
RewriteRule ^(.*)-a-(.*)$ article_info.php?articles_id=$2&% [QSA,L]
RewriteRule ^(.*)-au-(.*)$ articles.php?authors_id=$2&% [QSA,L]
RewriteRule ^(.*)-pr-(.*)$ product_reviews.php?products_id=$2&% [QSA,L]
RewriteRule ^(.*)-pri-(.*)$ product_reviews_info.php?products_id=$2&% [QSA,L]
RewriteRule ^(.*)-f-(.*)$ faqdesk_info.php?faqdesk_id=$2&% [QSA,L]
RewriteRule ^(.*)-fc-(.*)$ faqdesk_index.php?faqPath=$2&% [QSA,L]
RewriteRule ^(.*)-fri-(.*)$ faqdesk_reviews_info.php?faqdesk_id=$2&% [QSA,L]
RewriteRule ^(.*)-fra-(.*)$ faqdesk_reviews_article.php?faqdesk_id=$2&% [QSA,L]
RewriteRule ^(.*)-i-(.*)$ information.php?info_id=$2&% [QSA,L]
RewriteRule ^(.*)-links-(.*)$ links.php?lPath=$2&% [QSA,L]
RewriteRule ^(.*)-pm-([0-9]+)$ info_pages.php?pages_id=$2&% [QSA,L]
RewriteRule ^(.*)-n-(.*)$ newsdesk_info.php?newsdesk_id=$2&% [QSA,L]
RewriteRule ^(.*)-nc-(.*)$ newsdesk_index.php?newsPath=$2&% [QSA,L]
RewriteRule ^(.*)-nri-(.*)$ newsdesk_reviews_info.php?newsdesk_id=$2&% [QSA,L]
RewriteRule ^(.*)-nra-(.*)$ newsdesk_reviews_article.php?newsdesk_id=$2&% [QSA,L]
RewriteRule ^(.*)-po-([0-9]+)$ pollbooth.php?pollid=$2&% [QSA,L]

#Ultimate SEO END


Feel free to correct anything ive done ineffciently or wrong.

berrywoods

11:20 am on Jul 11, 2010 (gmt 0)

10+ Year Member



oh and forgot to add, change all the checkout files defined in filesnames.php to extensionless as well.

g1smd

11:59 am on Jul 11, 2010 (gmt 0)

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



There are several dozen errors and inefficiencies in there, including reverting to bad code that was already fixed several posts back. It's going to take a while to work through that lot and fix it.

There's a redirect (according to the comments) that's coded as a rewrite.

The [L] flag is missing from some rules.

The index redirect is inefficient and does not cover all cases.

The non-www to www redirect has several flaws. Redirecting all requests to root is also a mistake.

The non-www to www redirect must be the last redirect.

The redirects MUST all be listed before any of the rewrites.

The (.*) constructs have to go.

The comment
# Strip Extension and Generate Rule with a slash at end

does not match what that code actually does.

Several of the redirects create a chain for some requests. The code needs to be optimised to prevent that.

berrywoods

2:16 pm on Jul 11, 2010 (gmt 0)

10+ Year Member



ok, sorry as i said im new to htaccess and this is just what ive come up googleing what ive needed until it did what i needed. The Ultimate seo wasnt written by me built for a very old package so probably now being very out of date,

if anyone fancies helping optimise and correcting it id appreciate it. A lot of it ive researched rather than just posting a need without trying to have a go first.

berrywoods

3:09 pm on Jul 11, 2010 (gmt 0)

10+ Year Member



the basic URLS pan out like

/dogs-puppyjunior-pedigree-complete-puppy-10kg-chicken-rice-p-23

the p-** number is interchangable and will not change the url but will change the product. and related to

product_info.php?products_id=** of the same number. If thats any help as to why the new script doesnt work?

jdMorgan

1:04 am on Jul 13, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We've repeated the necessary clean-ups, but here it is again:

# Parse Extensionless Files for PHP includes
#
<FilesMatch "checkout_(shipping(_address)?|payment(_address)?|success|confirmation|process)">
ForceType application/x-httpd-php
</FilesMatch>
#
#Enable Engine and Allow Globals
#
php_flag register_globals on
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#
# Externally redirect direct client requests for "<anydirectory>/index.php" to "<anydirectory>/"
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/?#\ ]*/)index\.php([?#][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]*/)*)index.php$ http://www.example.co.uk/$1 [R=301,L]
#
# Externally redirect to add a slash if no slash on extensionless URL
RewriteCond $1 !\.[a-z0-9]{1,5}$ [NC]
RewriteCond $1 !checkout
RewriteRule ^(.*[^/])$ http://www.example.co.uk/$1/ [R=301,L]
#
# Externally redirect to extensionless URL with a trailing slash if the client
# requests a URL ending in ".php" that resolves to an existing file
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/\ ]+/)*[^.\ ]+\.php\ HTTP/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.berrywoods.co.uk/$1/ [R=301,L]
#
# Externally redirect to canonical hostname if requested hostname has no
# leading www or if it is in FQDN format or has a port number appended
RewriteCond %{HTTP_HOST} ^example\.co\.uk [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.co\.uk(\.|\.?:[0-9]+)$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk [R=301,L]
#
# Internally rewrite extensionless URL to php file if it
# exists and if no directory exists with the same name
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)/$ $1.php [L]
#
# Almost-Ultimate SEO (Optimized) START
# Internally rewrite 'friendly' URLs to CMS scripts
RewriteRule ^([^-]+-)+p-(.+)$ product_info?products_id=$2 [QSA,L]
RewriteRule ^([^-]+-)+c-(.+)$ index.php?cPath=$2 [QSA,L]
RewriteRule ^([^-]+-)+m-(.+)$ index.php?manufacturers_id=$2 [QSA,L]
RewriteRule ^([^-]+-)+pi-(.+)$ popup_image.php?pID=$2 [QSA,L]
RewriteRule ^([^-]+-)+by-(.+)$ all-products.php?fl=$2 [QSA,L]
RewriteRule ^([^-]+-)+t-(.+)$ articles.php?tPath=$2 [QSA,L]
RewriteRule ^([^-]+-)+a-(.+)$ article_info.php?articles_id=$2 [QSA,L]
RewriteRule ^([^-]+-)+au-(.+)$ articles.php?authors_id=$2 [QSA,L]
RewriteRule ^([^-]+-)+pr-(.+)$ product_reviews.php?products_id=$2 [QSA,L]
RewriteRule ^([^-]+-)+pri-(.+)$ product_reviews_info.php?products_id=$2 [QSA,L]
RewriteRule ^([^-]+-)+f-(.+)$ faqdesk_info.php?faqdesk_id=$2 [QSA,L]
RewriteRule ^([^-]+-)+fc-(.+)$ faqdesk_index.php?faqPath=$2 [QSA,L]
RewriteRule ^([^-]+-)+fri-(.+)$ faqdesk_reviews_info.php?faqdesk_id=$2 [QSA,L]
RewriteRule ^([^-]+-)+fra-(.+)$ faqdesk_reviews_article.php?faqdesk_id=$2 [QSA,L]
RewriteRule ^([^-]+-)+i-(.+)$ information.php?info_id=$2 [QSA,L]
RewriteRule ^([^-]+-)+links-(.+)$ links.php?lPath=$2 [QSA,L]
RewriteRule ^([^-]+-)+pm-([0-9]+)$ info_pages.php?pages_id=$2 [QSA,L]
RewriteRule ^([^-]+-)+n-(.+)$ newsdesk_info.php?newsdesk_id=$2 [QSA,L]
RewriteRule ^([^-]+-)+nc-(.+)$ newsdesk_index.php?newsPath=$2 [QSA,L]
RewriteRule ^([^-]+-)+nri-(.+)$ newsdesk_reviews_info.php?newsdesk_id=$2 [QSA,L]
RewriteRule ^([^-]+-)+nra-(.+)$ newsdesk_reviews_article.php?newsdesk_id=$2 [QSA,L]
RewriteRule ^([^-]+-)+po-([0-9]+)$ pollbooth.php?pollid=$2 [QSA,L]
# Almost-Ultimate SEO (Optimized) END

Note that all 'friendly' extensionless URL links on your pages must now have trailing slashes, in order to avoid a redirect based on the second rule. Its up to you to configure your CMS to add these slashes to your on-page links to avoid tanking your search rankings, and so that it will accept trailing slashes in the various script query parameters.

I'm not sure what was intended by the <Files> sections you added, since *files* on a server should always have an extension, even if the URLs mapped to them do not (which is rather the whole point here.) But regardless, I optimized them down to a single equivalent <FilesMatch> for the sake of efficiency.

Jim

[edited by: jdMorgan at 3:18 pm (utc) on Jul 14, 2010]

berrywoods

11:31 pm on Jul 13, 2010 (gmt 0)

10+ Year Member



Hi JD, just gave it a wirl. as we stand

the Filesmatch part for the checkout files for some reason was causing my server the hang, page wouldnt load. Testing the rest of script using my orginal hunk of junk.

The SEO script now works, only bit i had to change was

RewriteRule ^([^-]+-)+p-(.+)$ product_info?products_id=$2 [QSA,L]

to RewriteRule ^([^-]+-)+p-(.+)$ product_info/?products_id=$2 [QSA,L]

Thanks for that this is still all a learning curve for me

berrywoods

1:05 am on Jul 14, 2010 (gmt 0)

10+ Year Member



Right after more testing Only problems so far...

As said previous your file match thing causes server not to load. Does work with mine but i know its not correct.

the index.php C path is adding a space after the URL, doesnt cause a problem until on a cat page you click next page.

the URL then becomes

/small-animal-cages-c-907_978 ?page=2&sort=2a (This is with redirect to a slash page disabled, this still loads correctly)

this is with redirect to slash enabled

/small-animal-cages-c-907_978%2F ?page=2&sort=2a

websites usable with the slash redirect disabled, but for now example.co.uk/information wont work
while
example.co.uk/information/ will.



when enabled it becomes.

jdMorgan

3:23 pm on Jul 14, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There were a couple of typos in that FilesMatch, now corrected above. The line should be:

<FilesMatch "checkout_(shipping(_address)?|payment(_address)?|success|confirmation|process)">

I don't know about your "space being added" problem. Either it's a script problem or you've got multiple rules being applied to the request, and one of them is adding a space...

Rather than telling us what the post-click address bar looks like, it would be more useful to know what the link that you are about to click looks like in the browser status bar when you hover over that link... If there is a trailing space on it, then that problem needs to be fixed wherever it is initially occurring.

Jim

berrywoods

5:26 pm on Jul 15, 2010 (gmt 0)

10+ Year Member



With that new filesmatch rule i now get a 500 server error.

What i can see when looking over the link in status bar is the address with a space between the ? and its action, when the link is clicked this changes %2F (code for a slash) so im guessing the slash is causing problems here, ill have a play, if i cant fix it i shall just remove the slash from URL's

jdMorgan

1:44 am on Jul 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When you get a 500-Server Error, look at your server error log file. That makes it a lot easier to find (and to describe and discuss) problems.

Jim