I am having a problem with my htaccess file, and can't figure out if it's my configuration, or something like server cache which is messing with my URLs.
My file is the following
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^website.com$
RewriteCond %{HTTPS} off
RewriteRule (.*) https://website.com/$1 [R=301,L]
RewriteRule ^([a-z]+)\/?$ $1.php [NC]
The first rule is to move www to non-www, it works.
The second rule is to move http to https, it also works.
the third rule is to make URL /anyFile call anyFile.php, but keep a lovely URL.
The problem is that it calls anyFile.html, not PHP, and if I remove said HTML file then I get 404.
Yes it works typing anyFile.php in the URL, but I would like to not have .php in the URL.
If it is not obvious enough, it is supposed to work for any file name, not just a single one.
Any and all help is much appreciated.
Replace your .htaccess rules with this code:
Options -MultiViews
RewriteEngine On
RewriteBase /
# single rule for http->https and www removal
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://website.com/$1 [R=301,L]
# hide .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-z]+)/?$ $1.php [NC,L]
I'm recently learn how to do htaccess too, so I'm still a novice but I would write
RewriteRule ^([a-z]+)\/?$ helloWorld.php [NC]
to redirect it to the helloWorld.php file.
The $1 is a get parameter.
Related
I am redirecting traffic between old web and new web and I need to redirect 301 from /hotel.php?hotel=lasvegas&lng=es to http://www.new-web.com/hotel-las-vegas/
How could be possible from htaccess?
I have tried
Rewriterule ^hotel\.php\?hotel=lasvegas\&lng=es http://www.new-web.com/hotel-las-vegas/ [L,R=301]
I think my error is at regular expression. Can anyone help me?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /hotel\.php\?hotel=lasvegas&lng=es [NC]
RewriteRule ^ hotel-las-begas? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^hotel-las-vegas/?$ hotel.php?hotel=lasvegas&lng=es [L,QSA,NC]
it's very important that you insert best code of redirect 301 in best place of htaccess. for example :
http://www.abartazeha.com/
we replace abartazeha.com instead of abartazeha.ir
...
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
I am hosting multiple domains on one hosting account, so I have made folders in the public_html folder for each domain.
e.g. home/username/public_html/examplewebsite.com/
and
home/username/public_html/otherexamplewebsite.com/
I've been trying to figure out the code I need in my htaccess file to redirect visitors to the appropriate subfolder when they type in examplewebsite.com so that they don't see that they're in a subfolder of the main domain. I found this code originally, which was working:
Options -Indexes +SymLinksIfOwnerMatch
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/domainfolder/
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.
#Rewrite all those to insert /folder
RewriteRule ^(.*)$ /domainfolder/$1 [L]
but I also want to redirect all visitors who type in the naked domain to the www. version. I found this code, but when I use them both in the htaccess file you can see the subfolder name in the url again:
#Only www, no naked domain
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
I don't really understand much of all this code so If any of you gurus could help me figure out a solution I would really appreciate it!
Keep your rules like this in root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\. [NC]
RewriteRule !^domainfolder/ domainfolder%{REQUEST_URI} [L,NC]
I´m trying to redirect an entire folder which is in domain.com/folder/templates1/, to a maintenance .html page, however I´m getting into a redirect loop with the next code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !/folder/templates1$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /mypage.html [R=302,L]
</IfModule>
Can anyone check this and let me know how could I avoid that loop?
Thank you in advance.
Try this rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule !^maintenance\.html /maintenance.html [R=302,L]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/folder/templates1 [NC]
RewriteRule . /mypage.html [R=302,L]
</IfModule>
Should do it. It should send any request starting with /folder/templates1 to /mypage.html, with a 302 (temporary redirect) status. What were you trying to do with the image files? Do you want them exempted? That wasn't in your specification, but can be added with one line:
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif)$ [NC]
Of course, you're on a server (Apache?) that recognizes .htaccess, and the mod_rewrite is enabled.
I have put the following content in my .htaccess file. What I want is redirect all mydomain.com/ to www.mydomain.com/. At the same time I want subdomain.mydomain.com/ should not get redirected anywhere. With the below code, I am able to achieve my 1st goal. But it redirects http://subdomain.mydomain.com/ to http://www.mydomain.com/subdomain. Please guide me on how to achieve both my modifying .htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mydomain.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
Make it more restrictive, i.e. apply rule only when host name is mydomain.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
I spend the last 2 days searching and learning about .htaccess to redirect all subdomains to a user folder
without changing the url, but i keep having 3 little problems. I really would appreciate some help
problem 1)
If i go to http://www.example.com/users/foo it automatically redirect
the url to http://foo.example.com/ but if i go to http://www.example.com/users/foo/dashboard.php
it does nothing, instead of redirecting to http://foo.example.com/dashboard.php
this is the code i use:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /users/(.+)/\ HTTP/
RewriteRule ^users/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
problem 2)
if i go to http://foo.example.com/dashboard.php
it gets its information from http://www.example.com/users/foo/dashboard.php
but if i type foo.example.com in the address bar (in firefox) it (sometimes) automatically redirect
to www.foo.example.com, can i prevent the www prefix?
this is the code
RewriteCond %{REQUEST_URI} !^/users/
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com
RewriteRule (.*) /users/%1/$1 [L]
problem 3)
the campaign website is locaded in the folder http://www.example.com/website but should be
visible on the url http://www.example.com/ (like i get the information from http://www.example.com/users/foo
on the url http://foo.example.com/).
About problem one I would just check the host:
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^users/([^/]+)/(.*)$ http://$1.domain.com/$2 [R=301,L]
About problem second this is a modified rule which I got from the drupal .htaccess file:
# remove www prefix
RewriteCond %{HTTP_HOST} ^www\.(.+)\.domain\.com$ [NC]
RewriteRule ^ http://%1%.domain.com{REQUEST_URI} [L,R=301]
# redirect to the subdirectory
RewriteCond %{HTTP_HOST} !^www\.(.+)\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteRule ^ user/%1%{REQUEST_URI} [L,R=301]
About your third problem check this one here:
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^(.*)$ website/$1 [R=301,L]