Forum Moderators: rogerd & travelin cat

Message Too Old, No Replies

Wordpress creating htaccess file

         

lee_sufc

3:26 pm on Apr 23, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



I have a WP blog installed in the /blog/ directory of my site. Today, I noticed that if visiting the non-www version of the blog/ page, it redirected to my "main" site's home page? It didn't do this on any other blog pages / posts - just the blog's index page.

Anyway, after a little digging, it appeared that there was an additional htaccess file in my blog's folder - not one I created. I deleted this and hey presto, the redirect to www works fine throughout the site.

Being sure this had happened to me before, I noticed someone on this site had helped me a few years back and she stated that WordPress will create a new htaccess file each time something changes. This means that this file could have been created a while back and I've only just noticed. (Typically, I forgot this!)

A) Could this one error have impacted my site in Google? It was just the non-www version of the blog's indec page redirecting to my site's main home page - everything else worked as it should.
B) Other than manually having to remind myself to check, is there anyway I can stop WordPress creating this extra htaccess file?

Just to confirm; I've completely deleted the htaccess file from the /blog (WordPress) directory. This file shows the following:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

# END WordPress

[edited by: lee_sufc at 3:48 pm (utc) on Apr 23, 2017]

TorontoBoy

3:47 pm on Apr 23, 2017 (gmt 0)

5+ Year Member Top Contributors Of The Month



WP needs that htaccess in your /blog/ directory to properly direct your permalinks. I think WP will recreate it if you delete it. Your WP site's url should be example.com/blog/. What URL did you want to redirect to your non-www version?

WordPress uses this file to manipulate how Apache serves files from its root directory, and subdirectories thereof. Most notably, WP modifies this file to be able to handle pretty permalinks.
[codex.wordpress.org ]WP Codex

lee_sufc

3:51 pm on Apr 23, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



My site is in example.com/blog I want the non-www version to redirect to www.example.com/blog, but instead it redirected to www.example.com (the site's home page - not the blog section as intended).

Hope this makes sense.

martinibuster

5:11 pm on Apr 23, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The blog may be set to display at the wrong URL.

In the left hand navigation of your dashboard click the SETTINGS menu item and then edit the Site Address field to the correct location the blog should display at.

lee_sufc

5:19 pm on Apr 23, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi Martinibuster - the "WordPress Site" and "Site Address" are both shown as www.example.com/blog

I'm guessing this has been set wrong for years? I'm assuming the "Site Address" should show as www.example.com

If so, should I put the htaccess back and test out changing the "Site Address" - just worried about changing something and then breaking it.

TorontoBoy

6:17 pm on Apr 23, 2017 (gmt 0)

5+ Year Member Top Contributors Of The Month



If you change the WP address, WP will look for files in your public_html directory instead of /blog/ and your site will break. Not a huge issue, but a bit scary at first. You'll then need to manually edit your wp_config.php file to recover admin.

On the Settings->General screen in a single site installation of WordPress, there are two fields named "WordPress Address (URL)" and "Site Address (URL)". They are important settings, since they control where WordPress thinks your site is located. They control the display of the URL in the admin section of your page, as well as the front end, and are used throughout the WordPress code.

The "Site Address (URL)" setting is the address you want people to type in their browser to reach your WordPress blog.
The "WordPress Address (URL)" setting is the address where your WordPress core files reside.
[codex.wordpress.org ]

lee_sufc

6:18 pm on Apr 23, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



The WP address is correct - it's the Site address (where it says if the home is different to the blog's directory) - that's the one that's wrong as it currently shows the same as the blog - www.example.com/blog/ instead of www.example.com which is what it should show. Therefore, do you think it's going to be simpler if I change this? **Got my finger over the button to make the changes but am worrying like mad!**

not2easy

6:39 pm on Apr 23, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



WP makes its own htaccess file and it needs to have that to manage your permalinks. If you have the proper URL in your settings file, that is where it looks for its htaccess file. If that is deleted, you can put one back, it is a boilerplate piece of code:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

It can show from the root directory if it is set up that way, but if it is supposed to be in /blog/ then that's where it looks for the WP instructions (as shown above).

You may also have that in your root htaccess file and it should be after your www/non-www canonicalization rules. If you have a file in your root directory called "index.php" and no htaccess in /blog/ that will make WP look to the root directory's index.php as its own.

lee_sufc

6:43 pm on Apr 23, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



Cheers not2easy. I'll put the htaccess back in - which will then bring back the issue of the non-www version of example.com/blog redirecting to www.example.com. I'm then assuming changing the "Site URL" in WP's settings to www.example.com (currently www.example.com/blog) "should" solve the problem (the WP URL is currently correct). Am just wary of doing it, in case I break the blog completely and have to spend hours on a Sunday evening trying to fix it, lol.

not2easy

6:53 pm on Apr 23, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



The home address of your site does not need to be in the WP Settings file, it should only have the URL for the WP install (unless you are trying to run WP as the site's main page and have it installed in a different folder such as /blog/) It can be confusing to separate what it from what should be so I hope I've not confused it more.

If the htaccess snippet that is copied in the OP above has that
RewriteBase /blog/
in it, it should work fine from the root directory. When WP updates, it can rewrite a new htaccess if it does not find one in the folder of its Settings URL, that may happen again.

not2easy

6:55 pm on Apr 23, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



You can always add your canonicalization rewrite rules above the WP snippet in the blog folder. That is because those rewrites are not inherited from the root directory when they find another htaccess file in a folder.

lee_sufc

7:02 pm on Apr 23, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hmmm...think I'm getting more confused - easily done :-)

So, to clarify, in WP Admin settings, the WP Address (URL) should remain as www.example.com/blog....then site address (URL) should just be blank?

Then, I put the htaccess file back in the /blog folder that I deleted earlier (shown in my OP).

My main confusion is with the site's own htaccess file in the root folder - should I do anything to this to resolve my problem of the non-www version of /blog redirecting to www.example.com?

Apologies for the repetition of everything - just trying to make everything as clear as I can. Thanks also for everyone's patience in trying to help with this!

not2easy

7:07 pm on Apr 23, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I had not done this for so long, I went and looked it up. No wonder you're confused. The Wordpress resource for Settings [codex.wordpress.org] gives more detailed information and yes, if the site's main page is at example.com and the blog is at example.com/blog/ then you do need both URLs listed.

The only site I set up in a sub-folder uses the root as its URL, set up about 10 years ago, so I apologize for adding to the confusion. I just saw that "hand over the button" part and hit the keyboard. Sorry for any anxiety.

lee_sufc

7:10 pm on Apr 23, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



lol...not a problem! It's all foreign to me at the best of times - the instructions may as well be in a different language.

So...to clarify what you're saying (sorry!)

1) Put the htaccess back into the /blog directory.
2) Change the site URL to the "main" site's home page.

This has been an issue for me for years - I'm not sure how long the htaccess file has been there incorrectly redirecting that one page...am hoping it hasn't affected anything too much.

lee_sufc

9:58 am on Apr 24, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



I'll give the above a go later and let you know how I get on...with all my recent problems with WordPress, I think I might actually get rid of WP altogether and just re-upload the pages I have as standalone files (will be much simpler!)

not2easy

11:56 am on Apr 24, 2017 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



That sounds right. As mentioned, the WP URLs are constructed by WP internally using that Settings file and the htaccess file. Because that adds a htaccess file to a subfolder the www/non-www rewrite rules in the root directory are not inherited and you should have those rules repeated in the /blog/ folder's .htaccess file to reintroduce those rules. If you have lines to rewrite to www in your root directory .htaccess file similar to:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [OR]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
then you should also have
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [OR]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://www.example.com/blog/$1 [R=301,L]

in the /blog/ folder, above (or before) the WP part of the htaccess lines for WordPress.

Those lines may not be the same as you use if you have not changed to https, but those are the kind of rules that would get lost (not inherited) if you have a .htaccess file in the /blog/ folder.

TorontoBoy

1:05 pm on Apr 24, 2017 (gmt 0)

5+ Year Member Top Contributors Of The Month



WP has significant overhead and with 25% of the world's web sites, remains a huge target for bots. You need to spend sufficient time with WP to ensure it will not get hacked. Brute force wp-login and xmlrp attacks are the norm. If you do not need a CMS and you do not update content regularly, then consider moving away from WP.

Research static site generators (SSGs). I have used Hexo, but there are others. Basically these SSGs generate all the index pages for your site. You can change themes to your liking. These SSGs are really flat files written in markup, that have a converter to html. They are super light weight and very fast. You can then move your site to wherever using FTP.

lee_sufc

1:27 pm on Apr 24, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



@TorontoBoy - I have this morning been researching options and I think after some thought, I'm going to ditch WordPress. I don't use the commenting features at all and they are all just plain article-type pages with no plugins / anything being used. Issues like this with the htaccess have just pushed my decision to do this.

I might have to create a new post to ask this...however, will I be safe to simply create offline copies of each blog post (I already have a HTML theme so don't need to worry about that)...then, delete WP, delete databases and then re-upload all the individual files into their original location? This way, I'm assuming that in Google's eyes, nothing has changed?

Thanks again for everyone who has replied to this topic - given me food for thought!

dougwilson

2:44 pm on Apr 24, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



"...in Google's eyes, nothing has changed?"

site/blog (wordpress)/ and site/blog (no wordpress)/ are the same

lee_sufc

3:03 pm on Apr 24, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



@Doug - have you ever done this before? Other than simply deleting the WordPress folder and associated databases and then adding amending htaccess to remove the .php extension so the URLs match how they're shown on WordPress, is there anything I need to be aware of before uploading the standalone replacement pages?

Cheers mate.

TorontoBoy

3:29 pm on Apr 24, 2017 (gmt 0)

5+ Year Member Top Contributors Of The Month



I have had this WP epiphany with many other people. They are tired of WP's overhead and security issues, and don't use WP's CMS. There is a movement to move off WP for such situations, and it is strong. WP is a tool but not for everyone. WP should not be the default for a web site. In the past for many people it was.

Research WP to static site generator (SSG) converters. They are available. All they do is flatten your site. Then ensure your URLs match the WP URLs so search engines won't notice. Then take the WP site offline. Lots of people have the same issues you have with WP, so there is a lot of web content on how to migrate off WP.

I am an active WP publisher, so WP is for me.

lee_sufc

3:36 pm on Apr 24, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



That's excellent, thanks. In some ways, I'd feel more comfortable manually getting rid of WP rather than use an application. I have a handful of pages and it'll primarily be a copy and paste job for 90%+. I just want to make sure that before doing anything, I'm not missing something - the only thing I can see that would be different would be the removal to an RSS feed - everything else in the code for each page will be identical.

The only thing I need to research more is what to do with htaccess to ensure the php URLs display without the extension and show exactly as they do currently in WP.

TorontoBoy

3:44 pm on Apr 24, 2017 (gmt 0)

5+ Year Member Top Contributors Of The Month



Some of the SSGs have plugins that can do things like RSS. I know that the Hexo SSG has an RSS plugin. Commenting and dynamic page generation cannot be done, but RSS is pretty static. Generate your site, generate the RSS, done.

An SSG will have available themes, so your site can look modern, and you can still code in html and tweak the css.

lee_sufc

3:47 pm on Apr 24, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



Great - will take a look. My site already has a HTML template (I created the WP theme manually from this) so I don't need to worry about the template.

dougwilson

3:57 pm on Apr 24, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



Yes, I've done it. copied text to offline html template. Deleted all wordpress/database/folder/ ... recreated folder (same name) and uploaded.

Gotta keep the same folder name or you'll have to 301

Wordpress isn't really bad or all that difficult in my opinion. Wordpress themes and plugins can be

lee_sufc

2:57 pm on Apr 25, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



@dougwilson - one quick question (if you don't mind). How did you deal with getting the URLs to match the way they looked on WP? I'm using php files with seperate header and footer files (for easy updating later). However, the file names will obviously end with ".php" What's the easiest way for me to change this for the page to always end with a "/" as they do currently?

dougwilson

3:40 pm on Apr 25, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



What's the url of the wordpress page look like now?

lee_sufc

3:47 pm on Apr 25, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



www.example.com/blog/blospostexample/

So if i upload a standalone version in php, that'll become www.example.com/blog/blogpostexample.php

dougwilson

9:08 pm on Apr 25, 2017 (gmt 0)

10+ Year Member Top Contributors Of The Month



There is a lot about rewriting existing pages at [webmasterworld.com...]

But, if you put your (php or html) page together, save it with no extension and upload it, what do you see when you view it in your browser?