Forum Moderators: phranque

Message Too Old, No Replies

Edit htaccess

Help edit htaccess

         

Netlink

6:11 pm on Dec 10, 2008 (gmt 0)

10+ Year Member



Folks..

I have an old .htaccess which worked fine until I changed my hosting from shared to private. the host is still the same just a new server. here is the old file that was working

********************************
AddHandler server-parsed .php
<Files ~ "^[^\.]+$">
SetHandler application/x-httpd-php
</Files>
AddHandler application/x-httpd-php .php

********************************

It is over 5 years old so alot has changed in the mod_rewrite() world.

After the move to new server, all .php files load just fine. Any files that have to be rewritten load a blank page or an error page(depending on browser) I have an option in the admin section of the script to turn "Search engine Firendly" url's on or off. If I turn it off all links work fine but of course I get the ugly php pages and since I have .html pages that have been indexed for 4 years out there, people will get lots of errors.

How do I edit the above to work on the new server? The new server now gives me an option to turn mod_php 4 or 5 on of off in the c-panel. Any help will be appreciated. Thanks

jdMorgan

7:37 pm on Dec 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is nothing in the code you posted that uses mod_rewrite, or that could deliver friendly URL requests to a script for content generation.

The only thing that is there is the first line, which tells the server to parse any requested URL which does not contain a period (e.g. extensionless URLs) for PHP includes. But those URLs would still have to be resolved to an existing filepath somehow, and I see nothing that would do that.

It would appear that some of the original code has been lost or deleted. Or perhaps it was in an .htaccess file in a subdirectory, and that .htaccess file is now missing.

Jim

g1smd

7:54 pm on Dec 10, 2008 (gmt 0)

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



... or that code was in the old httpd.conf but isn't in the new one.

Netlink

12:45 am on Dec 11, 2008 (gmt 0)

10+ Year Member



Thanks for responding guys. have to poke around the script to see if any other .htaccess files exist in another folder. The option to turn on/off "search engine friendly url's" is in the script admin setion. Not sure if that is a sign there is another .htaccess file in another folder.

Anyway will post findings currently on the road

Netlink

5:16 pm on Dec 12, 2008 (gmt 0)

10+ Year Member



Ok guys, Checked my other folders and unfortunately for me found no other .htaccess files. Actually unzipped my script file and went through them again just in case it was deleted on the new server but no luck.

Plan B....

I have decided to come up with a new mod_rewrite, that could deliver friendly URL requests.
Here is what I have.

My whole new server is set to mod_php version 5.2.x. Domains using php as cgi pretty much
ignore this setting

I host several domains on that server but I can change any of the individual domains to use any
of these PHP settings

PHP 4.4.X
PHP 5.2.x
mod_php 5.2.x

The script I am trying to modify is the only script on its own domain.

Here is what I am trying to achieve. Use mod_rewrite to deliver friendly URL requests. Below are example of actual URL's and what they would look like under friendly terms. I have both Categories and Articles within those categories just to give a better idea.

CATEGORY NAME: Is Disco Dancing Still in
Actual ULR>>> www.mydomain.com/category.php?cat_id=8
Wish to be>>> www.mydomain.com/category/is-disco-dancing-still-in-8-9.html

CATEGORY NAME: Monkey TV Reviews
Actual ULR>>> www.mydomain.com/category.php?cat_id=30
Wish to be>>> www.mydomain.com/category/monkey-tv-reviews-30-1.html

CATEGORY NAME: Dancing Advantages
Actual ULR>>> www.mydomain.com/category.php?cat_id=14
Wish to be>>> www.mydomain.com/category/dancing-advantages-14-1.html

ARTICLES

ARTICLE TITLE: Vacations in Arizona
Actual ULR>>> www.mydomain.com/articles.php?art_id=254&start=1
Wish to be>>> www.mydomain.com/article/vacations-in-arizona-254-1.html

ARTICLE TITLE: Dancing in Puerto Rico
Actual ULR>>> www.mydomain.com/articles.php?art_id=222&start=1
Wish to be>>> www.mydomain.com/article/dancing-in-puerto-rico-222-1.html

ARTICLE TITLE:PHP Scripting Doomsday Looming
Actual ULR>>> www.mydomain.com/articles.php?art_id=1697&start=1
Wish to be>>> www.mydomain.com/article/php-scripting-doomsday-looming-1697-1.html

Thanks Again guys

jdMorgan

6:05 pm on Dec 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So you have two main groups of URLs and filepaths here: "category" and "article".

The "friendly" URL carries the category or article number and also the title of the page, which is not actually used to deliver content (more on this later). Article URLs additionally carry a start page number.

Taking the more-complicated of the two, you'd need a rule like this in example.com/.htaccess:


# Internally rewrite /article/<title>-<article-ID>-<start-page>.html URLs
# to /articles.php?art_id=<article-ID>&start=<start-page> script filepath
RewriteRule ^article/([^\-]+-)+([0-9]+)-([0-9]+)\.html$ /articles.php?art_id=$2&start=$3 [L]

You can use this as an example to come up with the simpler rule for categories.

Once you get the basic function working, it's important that URLs with invalid or bogus titles are either rejected with a 404-Not Found or 301-redirected to the correct URL. This includes any deviation at all in the URL's characters, punctuation, or case. Be sure to test this, and add that functionality if it is missing.

If you ignore the "title" field of the URL, then the potential exists for massive duplicate-content problems, either accidentally- or maliciously-created. Because the title field in the URL doesn't affect the rewrite to your scripts, the RewriteRules don't care whether it is valid or not, and in fact have no way to "know." So your script must check this value to determine whether it's valid as-is, can be fixed-up and 301-redirected to the correct URL, or must result in a 404-Not Found response.

If you have no other RewriteRules in your .htaccess file, you will need to add either both of these lines or only the second one before any RewriteRules:


Options +FollowSymLinks
RewriteEngine on

Jim

Netlink

8:09 pm on Dec 12, 2008 (gmt 0)

10+ Year Member



Thanks Jim. Working on it now. Was getting internal error when I first added it but that may have just been me adding stuff.
Added it just as is above without any of my other stuff and I just got a blank page no errors.
Added my other .htaccess back in but seems like the new code you gave me is just being ignored. Page loads as before No errors, but no "friendly" URLs as well. Just plain .php links

Netlink

8:38 pm on Dec 12, 2008 (gmt 0)

10+ Year Member



Ooops, correction to the above. Articles seem to be responding. Forgot I hadn't done "categories" yet. My fault

Netlink

9:12 pm on Dec 12, 2008 (gmt 0)

10+ Year Member



Jim you are my hero. Seems to be working at least in the test environment) which happens to be on the production server. I just need to clean it up per your advice above.

Seems like anything with "article" ID in it or "category" ID will resolve. If any of those are missing it is going to 404.

Did you say I could add something to redirect?

jdMorgan

9:58 pm on Dec 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, the script needs to look up the passed "articleID" in your database, retrieve the title for that article, and compare it to the one in the originally-requested "friendly" URL. Since that's a scripting issue, I'll refer you to our PHP forum for advice -- mine wouldn't be so great... :)

You could modify the RewriteRule to go ahead and pass the articleID to the script for checking -- that's one way to do it:


RewriteRule ^article/(([^\-]+-)+)([0-9]+)-([0-9]+)\.html$ /articles.php?art_id=$3&start=$4&[b]title=$1[/b] [L]

The article title will now be available in the "GET" string seen by PHP.

Alternately, you could check the Request_URI variable from within PHP, and parse out the article ID using a regular-expressions construct like the one shown here.

In either case, require an exact match to return content. Otherwise, output a server response header of Status: "301-Moved Permanently", and another one of Location: containing the canonical URL as previously looked-up in the database. I'm not sure how PHP handles these two headers; Status is handled a bit differently than Location. You should be able to readily find examples, though.

Jim

g1smd

10:07 pm on Dec 12, 2008 (gmt 0)

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



They are both enabled using HEADER statements in the PHP code.

If you omit the one that says "301" then you get a 302 redirect.

Make sure the redirect really is a 301 redirect.

Netlink

10:32 pm on Dec 12, 2008 (gmt 0)

10+ Year Member



Ok guys let me put on my php hut see what I can cook up.....Actually more like what I can break :-)

Netlink

12:43 am on Dec 13, 2008 (gmt 0)

10+ Year Member



Jim, g1smd
You guys were great. I think I can take this baby now and see what else I can do. Again thanks so much for all your help. Great Job!