Jump to content


Photo

301 Rdirect - about.html to about/


  • Please log in to reply
5 replies to this topic

#1 RobertS

RobertS

    Dedicated Member

  • Members
  • PipPip
  • 153 posts
  • Gender:Male
  • Location:Lino Lakes, MN
  • Experience:Beginner
  • Area of Expertise:Web Designer

Posted 04 December 2017 - 08:43 PM

Probably asked before but in htaccess, how do I go about implementing a 301 redirect from example: about.html (or about.php) to about/ (no file extension). 

 

Thank you. 



#2 BrowserBugs

BrowserBugs

    Unhinged

  • Privileged
  • PipPipPipPipPip
  • 2,138 posts
  • Gender:Male
  • Location:Surrey, UK
  • Experience:Intermediate
  • Area of Expertise:I'm Learning

Posted 05 December 2017 - 09:29 AM

You would need a RewriteRule for getting /about/ to load /about.html. As for 301 it's Redirect the old to the new be careful you don't make an infinite loop. 



#3 RobertS

RobertS

    Dedicated Member

  • Members
  • PipPip
  • 153 posts
  • Gender:Male
  • Location:Lino Lakes, MN
  • Experience:Beginner
  • Area of Expertise:Web Designer

Posted 08 December 2017 - 02:29 PM

Here is a link I found - https://www.rapidtab...-generator.html. Thoughts on it's accuracy?

​For a permanent 301 redirect solution on a Linux platform with GoDaddy, which is a better solution in your opinion?

​htaccess:

 

# Permanent URL redirect - generated by www.rapidtables.com
Redirect 301 /about.html http://www.myurl.com/about/

 

or php:

 

<?php
// PHP permanent URL redirect - generated by www.rapidtables.com
header("Location: http://www.myurl.com/about/", true, 301);
exit();
?>

​This is for a simple redirect from about.html to about/ without a change in domain name or host. 

 

The following would be added to the htaccess file to hide the page extensions:

PHP EXTENTION REMOVAL

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^\.]+)$ $1.php [NC,L]

 

HTML EXTENTION REMOVAL

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^\.]+)$ $1.html [NC,L]

 

​I assume this is correct? 
 



#4 BrowserBugs

BrowserBugs

    Unhinged

  • Privileged
  • PipPipPipPipPip
  • 2,138 posts
  • Gender:Male
  • Location:Surrey, UK
  • Experience:Intermediate
  • Area of Expertise:I'm Learning

Posted 08 December 2017 - 05:43 PM

Careful mate, bulk doing anything when discussing 1 page is like cracking a walnut with a sledge hammer. If the page is true .html then obviously the php won't work in this instance. Neither of those htaccess examples are 'cut and shut' if you don't understand them, seriously take a few moments to understand rather than looking for a copy and paste solution, they are rewrites and not 301 redirects, big difference. I'll break it down;

RewriteEngine On

This lets you rewrite urls using a RewriteRule. But missing IMO;

RewriteEngine On
RewriteBase /

Rewrite base here tells the server to use root down, meaning you can shorthand the redirect url without the full www. etc.

RewriteEngine On
RewriteBase /
RewriteRule ^/about/ /about.html

Ok rewrite rule; if the url is /about/ then load /about.html.

RewriteEngine On
RewriteBase /
RewriteRule ^/about/ /about.html
Redirect 301 /about.html /about/

This 'should' now redirect people hitting /about.html to /about/. That said this is untested in your environment, not 100% you won't get a 301 loop. Future reference to keep things simple make all .html pages end in .php even if they don't 'technically' use php as it opens up the opportunity to use php in the file if required at a later date.



#5 RobertS

RobertS

    Dedicated Member

  • Members
  • PipPip
  • 153 posts
  • Gender:Male
  • Location:Lino Lakes, MN
  • Experience:Beginner
  • Area of Expertise:Web Designer

Posted 08 December 2017 - 06:15 PM

Ok. So I can use the below to 301 redirect about.html to about/ (after of course addl code is used to hide the extension in the first place? 

 

Hide extension:

 

UrlRewrite (do so for each page in the directory?):

 

RewriteEngine On

RewriteBase /

RewriteRule ^/about/ /about.html

Redirect 301 /about.html /about/

 

And this should work on Linux servers? I appreciate your help. 

 

 

Also, if I am using redirects from nonwww to www as well as index.html to main url, is there a particular order all of this needs to be in? I'll have to add in redirect from http to https another time...

Edited by RobertS, 08 December 2017 - 06:29 PM.


#6 BrowserBugs

BrowserBugs

    Unhinged

  • Privileged
  • PipPipPipPipPip
  • 2,138 posts
  • Gender:Male
  • Location:Surrey, UK
  • Experience:Intermediate
  • Area of Expertise:I'm Learning

Posted 08 December 2017 - 06:27 PM

Extension removal is different than rewrites in a fashion. The example you give will drop .html off every .html file in favour of /filename/. This sort of bulldozer approach can come unstuck very quickly; for example I've got a folder called /images/, i make a page called /images.html, what will happen?

 

Mate pm me a real world example so I can see the current structure vs desired structure, i'd be able to help more if I knew more but you might not want to post your structure to the world ;)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users