0

This is the code in my.htaccess file

#force non-www

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?example\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]

This code redirects www.example.com to example.com

But whenever there is something ahead of the url

Example -

www.example.com/file-consumer-something

It redirects me to example.com

I want it to become example.com/file-consumer-something

Can someone help ?

  • 1
    This code should already be doing what you require (that's what the `%{REQUEST_URI}` bit does)? If this is a recent change then make sure you've cleared your browser cache. However, your code does more than simply redirect `www.example.com` to `example.com` - do you have multiple domains? Do you have any other directives in your `.htaccess` file? Do you have any other `.htaccess` files along the filesystem path? – MrWhite Jan 05 '17 at 11:51
  • 1
    Possible duplicate of [Generic htaccess redirect www to non-www](http://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www) – nyedidikeke Jan 05 '17 at 12:16
  • I am using shared hosting Cpanel have a look at my .htaccess file. Sorry It exceeds the limit here. http://s000.tinyupload.com/download.php?file_id=58892297715516577289&t=5889229771551657728991750 – ConfusedClown Jan 05 '17 at 12:20
  • "exceeds the limit"?! How big is this file? That download link gets blocked as malicious by my browser. – MrWhite Jan 05 '17 at 13:22
  • It exceeded the word limit see the .htaccess content from here https://drive.google.com/file/d/0B8cNo7CwEBq9clJyVi1kWU54Sk0/view?usp=sharing – ConfusedClown Jan 05 '17 at 20:38

1 Answers1

0

The code you posted already looks like it should work as required. However, it's possibly conflicting with existing (mod_alias / mod_rewrite) directives in your .htaccess file. It's also doing more than you seem to require, so can be simplified.

Try the following instead. This should go near the top of your .htaccess file.

RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
MrWhite
  • 43,179
  • 8
  • 60
  • 84