Forum Moderators: phranque

Message Too Old, No Replies

.htaccess Mod Rewrite problem!

         

lobas

2:06 pm on Sep 29, 2010 (gmt 0)

10+ Year Member



Hi,

Hope someone can help, will be most appreciated.

My .htacces hides the .php extension and adds a trailing slash.

The problem is it doesn't redirect people going directly to the file.php, I need to redirect this to /file/ or im going to get duplicate content problems.

Also my 404 errors stopped working for no existing files e.g. /file2323
or /file32423/ just gives a page not redirecting properly error by firefox

Heres my code

RewriteEngine On

RewriteRule ^(.*)/$ /$1.php [L]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.parkat.co.uk/$1/ [R=301,L]



THANKS!

g1smd

3:18 pm on Sep 29, 2010 (gmt 0)

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



Change the order of the REQUEST_FILENAME and REQUEST_URI lines in your redirect so that the REQUEST_URI is first. This will speed up the code.

Consider adding another pattern to your redirect, one that checks if the request URL has an extension, and to then not bother doing the -f "exists" check for those either.

Add the standard non-www to www redirect code after your existing redirect.

Move your existing rewrite so that it is listed after all of your redirects.

Finally, add a brand new redirect ahead of all of the existing redirects. This will have an additional RewriteCond looking at THE_REQUEST. If it contains a request for file.php then redirect to www.example.com/file/. The RewriteCond is vital to stop an infinite redirect-rewrite-redirect loop.

Make sure you add a # comment before each line of code explaining what the next few lines do.

lobas

3:26 pm on Sep 29, 2010 (gmt 0)

10+ Year Member



Ahh I don't know how to do that :(

g1smd

5:16 pm on Sep 29, 2010 (gmt 0)

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



Luckily it's a topic that comes up every week, so there are several thousand prior examples, as well as many useful threads in the forum library.

lobas

6:31 pm on Sep 29, 2010 (gmt 0)

10+ Year Member



I have been looking nothing specific to mine about the firefox thing

jdMorgan

2:09 pm on Sep 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Problems: Rule order is incorrect. The very-inefficient "file exists" check is not required.

RewriteEngine on
#
# Externally redirect direct client requests for script filepaths back to "friendly" extensionless URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]*/)*[^.]+\.php([?#][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]*/)*[^.]+)\.php$ http://www.parkat.co.uk/$1/ [R=301,L]
#
# Externally redirect to add missing trailing slashes to extensionless URL requests
RewriteCond $1 !\.[a-z]+[0-9]?$
RewriteRule ^(.*[^/])$ http://www.parkat.co.uk/$1/ [R=301,L]
#
# Externally redirect all non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} !^www\.parkat\.co\.uk$
RewriteRule ^(.*)$ http://www.parkat.co.uk/$1 [R=301,L]
#
# Internally rewrite all requests ending with slash to php scripts
RewriteRule ^(.*)/$ /$1.php [L]

"THE_REQUEST" is the entire HTTP request line as sent by the client browser or SE robot, exactly as it appears in your raw server access log file. Example:
GET /dir123/file2323.php?parm1=abc&parm2=def#TOP HTTP/1.1

So the rewritecond pattern in the (new) first rule accommodates the HTTP method (GET), optional directory-path, page-name, optional query string and named anchor, and requested HTTP protocol.

Note that all external redirect rules go first, in order from most-specific to least-specific URL-patterns and condiitons, followed by all internal rewrite rules, again in order from most- to least-specific.

Delete your browser cache before testing any new code on your server.

Jim

[edited by: jdMorgan at 3:00 am (utc) on Feb 18, 2011]

lobas

5:51 pm on Sep 30, 2010 (gmt 0)

10+ Year Member



Thanks jdmorgan a big thanks for helping me with this you code works perfect.

lobas

4:00 pm on Oct 1, 2010 (gmt 0)

10+ Year Member



I have another problem now my folder pages dont load e.g. [somesimte.com...]

the index.php within /some-folder/ wont load but the pages within it like /some-folder/page/ (page.php) work fine

jdMorgan

7:08 pm on Oct 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What URL are you requesting in order to load "the index.php within /some-folder/"?

By adding a trailing slash to extensionless URLS as done by yourorignal code and my improved version, you have essentially declared that all "index page" URLs are to be rewritten to your scripts.

So the problem is not one of coding, but of your URL design.

I suggest that if you want to rewrite a "page URL" to a script, that the page URL should not have a trailing slash, and if you want to load the index page in a directory, then that URL should have a trailing slash.

example.com/page is a page, rewrite to page.php
example.com/pages/ is a directory, invoke example.com/pages/index.php

As it is now, your page and directory URLs are conflicting with each other.

Jim

lobas

8:18 pm on Feb 15, 2011 (gmt 0)

10+ Year Member



Sorry to bring this up again but the .htaccess is now causing duplicate content google has started to index urls in this format

http://www.mysite.co.uk/?http://www.mysite.co.uk/ 


And im not sure what to do i tried but had no luck

g1smd

8:23 pm on Feb 15, 2011 (gmt 0)

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



Someone asked the exact same question in the last week or so.

Check that thread for example code.

lobas

8:26 pm on Feb 15, 2011 (gmt 0)

10+ Year Member



That was me, I've tried what you mentioned but i still cant solve the problem, I sent you a pm could you replt please :P

jdMorgan

12:42 am on Feb 18, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Post your code. Post the URLs you tested with. Post the results you got. Post the results you wanted.

Jim