homepage Welcome to WebmasterWorld Guest from 204.236.254.124
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Need A 301 redirect from .php extension
glennk




msg:4083879
 10:50 am on Feb 20, 2010 (gmt 0)

I need to redirect roughly 100 urls in my route folder. They need a 301 redirect from .php to / extension

ie

www.example.com/example.php to www.example.com/example/

www.example.com/example2.php to www.example.com/example2/

www.example.com/example3.php to www.example.com/example3/

www.example.com/example4.php to www.example.com/example4/

www.example.com/example5.php to www.example.com/example5/

[edited by: jdMorgan at 5:57 pm (utc) on Feb 20, 2010]
[edit reason] example.com [/edit]

 

g1smd




msg:4083885
 11:32 am on Feb 20, 2010 (gmt 0)

Are the new URLs for folders?

Folders end in a trailing slash.

If the new URLs are merely 'extensionless', then there should not be a trailing slash.

One line of RewriteRule code can handle the whole thing for all of the URLs; depending on whether the site uses URL rewriting or not, you might also need a RewriteCond paired with that new RewriteRule.

glennk




msg:4083908
 1:47 pm on Feb 20, 2010 (gmt 0)

HI mate,

Sorry I do not fully understand 301 although I do understand the necessity.

The urls arent for folders but they are what are created by wordpress at the present time.

Maybe I have got the settings wrong in wordpress ?

glennk




msg:4083911
 1:54 pm on Feb 20, 2010 (gmt 0)

Hi there,

I removed the trailing slash in the parmalinks section. I now need a 301 as described above but without the trailing slash.

This is what resides currently in the .htaccess

deny from 77.88.24.0/22
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

redirect 301 /north-east-fishing-news/ http://www.example.com/category/fishing-news/
redirect 301 /kayak-fishing/ http://www.example.com/category/kayak-fishing/
redirect 301 /kayak-fishing-reviews/ http://www.example.com/category/kayak-reviews/
redirect 301 /kayak-launch-sites/ http://www.example.com/category/kayak-fishing/kayak-launch-sites/
redirect 301 /fishing-tackle-reviews/ http://www.example.com/category/fishing-tackle/

[edited by: jdMorgan at 5:58 pm (utc) on Feb 20, 2010]
[edit reason] example.com [/edit]

g1smd




msg:4083948
 4:11 pm on Feb 20, 2010 (gmt 0)

Firstly never mix Redirect and RewriteRule in the same .htccess file. If you use RewriteRule for some of the rules, use it for all of your rules. You'll need to recode the redirects.

Secondly, list the redirects before the rewrites. Redirects affect URLs. It's too late to properly affect URLs after you have done an internal rewrite; you end up exposing internal filepaths back out into external URLs.

Lastly, check out recent threads for a better replacement ruleset for wordpress, one that speeds up the rule processing by more than 100%.

glennk




msg:4083954
 4:18 pm on Feb 20, 2010 (gmt 0)

OK I dont need those rewrites anyway so they can go. Are you able to advise on rewriting the .php files please ?

glennk




msg:4083956
 4:21 pm on Feb 20, 2010 (gmt 0)

I have 100 urls like this and they are currently giving 404

http://www.example.com/bass-fishing-methods.php

i need them redirecting as its affecting my traffic through google. They need redirecting to :

http://www.example.com/bass-fishing-methods

[edited by: jdMorgan at 5:59 pm (utc) on Feb 20, 2010]
[edit reason] example.com [/edit]

g1smd




msg:4083973
 5:31 pm on Feb 20, 2010 (gmt 0)

It's a simple RewriteRule. On the left will be the pattern matching the path part of the URLs you want to redirect. On the right will be the redirect target URL with protocol and domain name and the captured part of the original URL that you wish to re-use in the new URL. Status flags [R=301,L] will finish it off. There's tens of thousands of code snippets with those elements in this forum.

jdMorgan




msg:4083988
 6:27 pm on Feb 20, 2010 (gmt 0)

Here's a total replacement.

As g1smd recommended, the mod_alias directives are replaced with mod_rewrite directives, so that execution order can be controlled/predicted, and the WordPress code has been optimized (two options shown). In addition, a new rule has been added to redirect non-canonical hostname requests to the canonical hostname, and another to redirect client requests for URLs ending in .php to remove that .php.

Everything that is different or has been added or removed is entirely intentional.

Deny from 77.88.24.0/22
#
RewriteEngine On
RewriteBase /
#
# Externally redirect specific "old" URLs to new ones
RewriteRule ^north-east-fishing-news/?$ http://www.example.com/category/fishing-news [R=301,L]
RewriteRule ^kayak-fishing/?$ http://www.example.com/category/kayak-fishing [R=301,L]
RewriteRule ^kayak-fishing-reviews/?$ http://www.example.com/category/kayak-reviews [R=301,L]
RewriteRule ^kayak-launch-sites/?$ http://www.example.com/category/kayak-fishing/kayak-launch-sites [R=301,L]
RewriteRule ^fishing-tackle-reviews/?$ http://www.example.com/category/fishing-tackle [R=301,L]
#
# Externally redirect [b]only[/b] direct client requests for old ".php" URLs to new extensionless URLs
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://www.example.com/$1 [R=301,L]
#
# Last external redirect - Redirect all non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# Optimized internal rewrite to WordPress script
# Exclude index.php itself from the file- and directory-exists check
RewriteCond $1 !^index\.php$
#
# NOTICE: Use only one or the other of the following RewriteConds. The first is most efficient, but
# you may need to use the more-specific second RewriteCond if your site won't work with the first.
#
# Exclude any requested URL ending with a filetype from the "exists" checks
RewriteCond $1 !^\.[a-z0-9]+$
# Exclude specific filetypes from the "exists" checks
RewriteCond $1 !^\.(gif|jpe?g|css|js|png|ico)$
# Read the disk to see if the requested URL resolves to an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*[^/])$ /index.php [L]
# END WordPress

The tweaks to the WP rewrite code will improve your server performance by more than 50% -- possibly enough to notice when your site gets busy. It does so by avoiding the extremely resource-intensive and slow disk checks unless actually required.

Jim

[edited by: jdMorgan at 4:06 am (utc) on Feb 22, 2010]

glennk




msg:4083994
 6:37 pm on Feb 20, 2010 (gmt 0)

Hi Jim,

Might sound a daft question, but I am daft.

Do I need to replace example.com with my domain name ?

Cheers - Glenn

glennk




msg:4083996
 6:50 pm on Feb 20, 2010 (gmt 0)

Hi Jim,

Sadly neither worked. The first left me with no images and only able to view the homepage. The second left me with images on the home page, but again the homepage was the only 1 accessible with all other pages going 404.

g1smd




msg:4084010
 7:12 pm on Feb 20, 2010 (gmt 0)

The image loading problems are probably due to using relative links in the hrefs on your pages. Change to 'full image path beginning with slash' and they will likely return to visibility.


Use 'Live HTTP Headers' to check exactly what is being requested from your server and what the responses to those requests really are.

glennk




msg:4084011
 7:16 pm on Feb 20, 2010 (gmt 0)

Is there a simple line I can put into the already existing htaccess to redirect php to trailing slash.

g1smd




msg:4084013
 7:31 pm on Feb 20, 2010 (gmt 0)

Yes, it is possible, but trailing slash represents a physical folder on the server hard drive. Is that what you really want?

This affects the 'prefix' added to relative URLs when the browser evaluates where related files (images, CSS, etc) are located.

"Extensionless" URLs have no extension and no trailing slash for good reason.

example.com/some-resource - is an extensionless page in the root of the site.

example.com/some-resource/ - is an index page in the /some-resource/ folder.

Even though the URLs used out on the web do not have .php on them, do be aware that the real files on the server do need to have .php on the end of the filename so that the server knows what sort of file they are.

glennk




msg:4084025
 8:14 pm on Feb 20, 2010 (gmt 0)

Sorry I really am lost. What I need to do is redirect pages in the following fashion

www.example.com/example.php to www.example.com/example

www.example.com/example2.php to www.example.com/example2

www.example.com/example3.php to www.example.com/example3

www.example.com/example4.php to www.example.com/example4

www.example.com/example5.php to www.example.com/example5

Is there any reason why I shouldnt do this as it really needs doing as all my urls in google are .php and I need them redirecting to their new home so my site visters arent met with the 404 error.

I am sorry if I am not explaining this very well.

glennk




msg:4084326
 4:15 pm on Feb 21, 2010 (gmt 0)

I have found a wordpress redirect plugin which will allow me to do this. The down side is that I will have to set up 100 individual redirects which is a bit of a shame if it could have been done on one line :

[wordpress.org...]

jdMorgan




msg:4084558
 4:07 am on Feb 22, 2010 (gmt 0)

Fixed typo in first RewriteCond of last rule above.

That may help.

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved