Forum Moderators: phranque

Message Too Old, No Replies

Forum-Friendly Links and .htaccess

         

jack13580

4:15 am on Jun 17, 2011 (gmt 0)

10+ Year Member



i have decided to make my links on my forum google friendly and get it over with so i came up with this .htaccess script

RewriteEngine On
Options +FollowSymLinks
RewriteRule ^(.*)/ /index.php?act=forum&id=$1

ErrorDocument 404 /index.php?act=404

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php


the first part starts rewite engine seconed part i heard is a good practice to have third part makes it so that [the-forum.net78.net ] will work if i put [the-forum.net78.net ] but my style sheet doesnt work if i use the new link and the table borders dont show up how do i fix this? i realy do not know much about .htaccess all i know is the basics of the basics
the fourth part is my custom error page the fith sixth and 7th part makes it so i dont have to use .php just the page name not .php extension which also has a glitch the glitch is that it at times work but at times does not and it sometimes doesnt show the page correctly

can someone please help me with this

lucy24

7:39 am on Jun 17, 2011 (gmt 0)

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



but my style sheet doesnt work if i use the new link and the table borders dont show up how do i fix this

That's the easy part. Although your user is physically on the page you sent them to, the server thinks they are on the page they asked for. That's what "rewrite" means.

So if you're using relative links for stylesheets or images, they have to be relative to what the user originally asked for. The alternative is to use absolute links: the kind that begin with / followed by the first directory. (The disadvantage to this method is that nothing works if you're testing it at home from files on your hard drive. I avoid it for this reason alone ;))

g1smd

8:20 am on Jun 17, 2011 (gmt 0)

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



It is the browser that resolves the URL for the stylesheet.

Begin the link with a slash and use the full path to the file in the link, e.g. "/styles/sheet.css" or similar. Do the same for images and for JS files too.

To test at home, install XAMPP or similar, where your site can be seen at http: //localhost/

g1smd

6:55 pm on Jun 17, 2011 (gmt 0)

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



You also need to add the [L] flag to both rules.

Move the ErrorDocument directive so that it is at the very beginning or the very end of the file.

jack13580

10:30 pm on Jun 17, 2011 (gmt 0)

10+ Year Member



such an easy fix and i had to go to 3 different forums to get an answer thanks but how do i fix this part

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php


it only works 25% of the time and when it does work it doesn't show the page correctly it is supposed to get rid of the .php file extension and one last thing with the thing that rewites index/php?act=forum&id=1 to mysite/1/ how do i make it so that if you put like mysite/1/something it gives a 404 error?

lucy24

5:00 am on Jun 18, 2011 (gmt 0)

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



it is supposed to get rid of the .php file extension

Did your cat walk across the keyboard? What you pasted in would actually add a php extension-- but only if there is already one there.

how do i make it so that if you put like mysite/1/something it gives a 404 error

A 404 means "no such page". If you're not getting a 404 in response to /anyoldcrap or /meaninglessnonsense.html, it means that one of your rewrites is grabbing everything in sight and rewriting it to something that is valid.

Some sites do rewrite all 404s to their front page (as a user, I hate this) so the user never sees the error message. But if it's correctly coded, the 404 has been logged.

jack13580

4:43 am on Jun 19, 2011 (gmt 0)

10+ Year Member



let me explain in better detail this is the .htaccess code

RewriteEngine On
Options +FollowSymLinks

#try to hide the .php extension but does not work
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

# force www. in all requests
RewriteCond %{HTTP_HOST} ^the-forum.net78\.net [NC]
RewriteRule ^(.*)$ http://www.the-forum.net78.net/$1 [L,R=301]

#the 404 error page
ErrorDocument 404 /index.php?act=404

#the members profile page
RewriteRule ^members/(.*).html /index.php?act=profile&id=$1 [L]

#view the forum sub directories that are in the main directory selected
RewriteRule ^view/(.*)/ /index.php?act=view&id=$1 [L]

#makes it so the pagination in the topics page works and is google friendly
RewriteRule ^(.*)/(.*)-(.*)-page-(.*).html /index.php?act=topic&forum=$1&id=$2&page=$4 [L]

#the topics pages
RewriteRule ^(.*)/(.*)-(.*).html /index.php?act=topic&forum=$1&id=$2 [L]

#the forum sub directory pages
RewriteRule ^(.*)/ /index.php?act=forum&id=$1 [L]


and i know i didnt add much but i had to change a lot of things around in my php code but anyway the part that is supposed to hide the .php extension does not work let me give you an example if i put in index without the .php it is supposed to work but does not it just rewrites the url to the forum subdirectory page because it thinks it is /index/ and without the rewrite rule it just gives me my 404 page

also how do i make it so that when a user is in my forum to view topics example url is the-forum.net78.net/General-Chat/ and the user puts in a page after that like General-Chat/random-page how do i make it so it takes them to my 404 page and not to rewrite them to the dang forum subdirectory page

one last thing i don't know why but if i don't have the rewrite rules in this exact order none will work like i want like the forum subdirectory one seems like it owns the topic and paginaton-topic rules and the topics page will own the pagination-topics page if i put it ontop of the pagination and the forum subdirectory one would also own the members-profile one and the view rule how do i fix this so they all work in no matter what order they are in?

g1smd

6:12 am on Jun 19, 2011 (gmt 0)

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



What do you mean by "hide" the extension?

If you are linking to URLs ending in .php then this is a problem. URLs are defined in links. It is too late to change the URL after the link is clicked.

If you want to use extensionless URLs then you link to extensionless URLs on the page. After the link is clicked the browser sends an extensionless request to the server. The server then rewrites that to be served by a .php file.

The (.*) pattern says "grab ALL of the input right to the end at once". You can't have multiple (.*) patterns in one rule. You MUST change them to ([^/]+/) or ([^-]+-) as appropriate.

The simple rule is this. Look at each (.*) in turn. If there is something to the RIGHT of it, other than a $ symbol, then (.*) is the WRONG pattern.

You MUST list the redirects before the rewrites otherwise the redirects expose the rewritten path as a new URL. You MUST add the [L] flag to every rule otherwise the parsing goes on to match other rules and you end up with major problems.

Use the Live HTTP Headers extension for Firefox and you'll soon see why the current code fails.

You must also escape the literal periods in the patterns. The ErrorDocument directive should be the very first, or very last, line in the file.

Additionally, where the target begins $1 you should add a leading slash to control the path mapping.

The rule order is VERY important. You must list redirects first in order from more specific to more general, with the non-www to www redirect last. After the redirects, list the rewrites. Again, these must be in order from most specific to most general.

lucy24

7:17 am on Jun 19, 2011 (gmt 0)

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



how do i fix this so they all work in no matter what order they are in?

You can't. It's not like "Allow,Deny" where all the Allows get processed before all the Denys, no matter what physical order they're in. It's more like an assembly line or conveyor belt: each request moves down the page from top to bottom until it meets an [L].