Forum Moderators: phranque

Message Too Old, No Replies

Need to accept www and non www requests for url

have redirects set up for subfolder but only to www

         

Egbert Souse

10:12 pm on May 18, 2016 (gmt 0)

10+ Year Member



Hi!

First a little background.

I am using WordPress in a sub directory
I am using cubecart (shopping cart) in a separate subdirectory

So the structure looks like this:
public_html
.htaccess
wp (directory)
cart (directory)

I had a previous post and thread back in December
[webmasterworld.com...]

Since everything is now being redirected to www
I have gotten numerous 404 errors and lost position on most of the top 10 google ranking pages I had.

I need to be able to accept both www and non-www I really don't care if google is going to penalize me.

This is what my .htaccess looks like now:

<Files .htaccess>
order allow,deny
deny from all
</Files>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>

# Force redirect to https top level
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# End top level redirect

# BEGIN HTTPS Redirection Plugin
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
# END HTTPS Redirection Plugin

# BEGIN moving pages to wp
RewriteRule ^([\w-]+)\.php http://www.example.com/wp/$1 [R=301,L]
# END moving pages to wp

#BEGIN strip .php extension
RewriteRule ^(newdir/[^.]+)$ /$1.php [L]
#END strip .php extension

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

# END WordPress

<Files 403.shtml>
order allow,deny
allow from all
</Files>

deny from 41.0.0.0/8
deny from 91.0.0.0/8


What do I need to change to rewrite and accept both www and non www requests for a url?

Thank You!

[edited by: bill at 4:19 am (utc) on May 19, 2016]
[edit reason] use EXAMPLE.COM [/edit]

lucy24

12:06 am on May 19, 2016 (gmt 0)

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



# Force redirect to https top level
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# End top level redirect

# BEGIN HTTPS Redirection Plugin
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
# END HTTPS Redirection Plugin
That would appear to be the same rule twice. Why are you using a plugin if you've got the knowhow to write the rule yourself? (You obviously do, or you wouldn't be posting here.) Except that there's no point in capturing if you're just going to say %{REQUEST_URI} all the same. And psst, plugin authors: ^(.*)$ is superfluous. A simple (.*) does exactly the same thing.

How many possible values can %{HTTP_HOST} have? In general you want to avoid the HTTP_HOST locution, because it doesn't fix domain-name issues, notably the with/without www element, as well as the rare request that comes in with an explicit port number. Don't quite see why this would lead to 404s, though. I'd make separate rules for the subdomains, and then you can mop up the residue with a single rule that both sets https and sends everyone to with/without www according to preference.

Incidentally...
<Files .htaccess>
order allow,deny
deny from all
</Files>
The mere existence of htaccess implies shared hosting. If you have reason to suspect that your host isn't already blocking all requests for files in .ht (it's part of the Apache default config file), you may have bigger problems than any of us can deal with.

Egbert Souse

6:06 pm on May 19, 2016 (gmt 0)

10+ Year Member



Thank you for your reply Lucy!

So basically I would create .htaccess files in each of these folders

http://www.example.com/wp/
http://www.example.com/cart/

to allow for either a www or no www for the url?

lucy24

7:34 pm on May 19, 2016 (gmt 0)

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



You should do everything in your power to avoid multiple instances of mod_rewrite along the same path. But it may not be possible if you're using a CMS in a subdirectory.

Where is your current WordPress install located? In the /wp/ subdirectory, as suggested by the name, or in the root? (I mean the physical location of the files, not the final URL.) And what about /cart/ ? I gather it uses a different CMS, but is it also based on mod_rewrite, as WordPress is? Does the same site have other subdirectories that use neither WP nor the shopping cart?

not2easy

8:23 pm on May 19, 2016 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I second lucy24's advice about avoiding multiple rewrites along the path. Remember that the WP URL is controlled in the settings file. Whatever is set there will produce URLs in that format - and you would need to have either www or non-www set in the settings file. An additional .htaccess in the /wp/ directory might very well cause 500 errors because it could be getting conflicting rules from the root htaccess, the local htaccess and the settings file.