December 4, 20178 yr 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.
December 5, 20178 yr 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.
December 8, 20178 yr Author Here is a link I found - https://www.rapidtables.com/web/tools/redirect-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.comRedirect 301 /about.html http://www.myurl.com/about/ or php: <?php// PHP permanent URL redirect - generated by www.rapidtables.comheader("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?
December 8, 20178 yr 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.
December 8, 20178 yr Author 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 December 8, 20178 yr by RobertS
December 8, 20178 yr 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
Create an account or sign in to comment