Htaccess Directs to Online When I Want to Work On Localhost - .htaccess

I changed my localhost server Apache to XAMPP. After then, my project has started to go to online site yusufcode.com(my website link) while I want to work on localhost. Where is the problem? This was the first question.
My second question is my links not works without .php extension on localhost.
Htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([a-z]+)\/?$ $1.php [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yusufcode.com/$1 [R,L]

Oh... I found the problem. The problem is the localhost port number.
I changed the port number from:
RewriteCond %{SERVER_PORT} 80
To this:
RewriteCond %{SERVER_PORT} 443
And all problems has solved. Thanks stackoverflow again. This platform helps me always, even nobody not answered :)

You should write it like this instead:
Options -MultiViews
RewriteEngine on
# Redirect to HTTPS (and www) but only on "live" site
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(?:www\.)?(yusufcode\.com) [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
# Handle extensionless .php files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]
There were a number of issues with your original directives that handled the PHP-extensionless URLs... the second RewriteRule ran unconditionally. The file check was potentially testing a different file that the one being requested.
UPDATE: This of course does assume that .htaccess overrides are enabled in the server config on your local Apache server. This is not the default on Apache 2.4. .htaccess overrides are enabled with the AllowOverride directive.
For example, somewhere in your server config you need something like this:
<Directory /path/to/document-root/>
# Allow access
Require all granted
# Allow all htaccess overrides
AllowOverride All
</Directory>
The default is AllowOverride None.

Related

how to force website to open with www and https with htaccess

I am unable to direct my example.in to https://www.example.in
I am using .htaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if
not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
AcceptPathInfo On
I have found this code on stackoverflow but it's not working in my project
my site is on AWS and using virtual host of apache2 php 7.0
It is not forcing www and and i have tried other code .htaccess but if i manually delete the www from url it is not redirected to www .
Thanks guys for support ,
I have solved my problem it was because ssl was install on my site and hence in /etc/apache2/sites-available/
I found default-ssl.conf file which contains some of setting which was, i guess not allowing me ovveride with .htaccess .
but now after adding
<Directory /var/www/myfolder > AllowOverride All </Directory>
The htaccess is working fine.
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

.htaccess http to https causes too many redirects error

First of all I'd like to say that I've re-read a lot of articles on this topic but my problem persists.
I have a Yii2-based project using advanced template, but I believe the problem doesn't apply to framework.
My .htaccess files is:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
# www to none www
RewriteCond %{HTTP_HOST} ^www.fashion-megapolis.com$ [NC]
RewriteRule ^(.*)$ https://fashion-megapolis.com/$1 [R=301,L]
# Force SSL
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</IfModule>
My www to none www rules works great. If I remove a force ssl part - my site works as expected, but whithout HTTPS of course.
So, these rules are very common and I tried lots of variations but get a "too many redirects" error all the time.
Thank you in advance

Transparently redirect a domain to a subdirectory

I have set up the DNS Zone of my domain towards the IP of my VPS.
When I type exemple.com I reach the httpdocs folder (the root folder) of the vhost wanted.
However in this folder there are few dirctories
httpdocs
-subfolder1
-Ssub-folder
-subfolder2
I would like :
1) that any request directed toward exemple.com be redirected toward exemple.com/subfolder1/ or idealy exemple.com/subfolder1/Ssubfolder/
2) and that the url displayed remains exemple.com
3) moreover, I also want to be able to acces to subfolder2 while typing exemple.com/subfolder2 or while typing my-IP-adresse/subfolder2
I made myself sure that the apache2.conf file contains AllowOverride All, and tried to edit the .htaccess file located in the httpdocs folder in many many ways, for instance :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^exemple\.com$
RewriteRule (.*) http://www.exemple.com/$1 [R=301,L]
RewriteRule ^$ /subfolder1 [L,R=301]
or
Options +FollowSymLinks
RewriteEngine on
RewriteRule !^subfolder1($|/) http://exemple.com/subfolder1/index.php/$1 [R=301,L]
Both redirect me correctly, but the url displayed is exemple.com/subfolder/
I've also tried
RewriteEngine on
RewriteRule (.*) http://exemple.com/subfolder/$1 [QSA,R=301,L]
But my url becomes http://exemple.com/subfolder1/subfolder1/subfolder1/subfolder1/subfolder1/subfolder1/subfolder1/subfolder1/subfolder1/subfolder1/subfolder1/subfolder1/subfolder1/subfolder1/subfolder1/subfolder1/subfolder1/subfolder1/subfolder1/
When I try this
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/subfolder1/
RewriteCond %{HTTP_HOST} ^(www\.)?exemple\.
RewriteRule ^(.*)$ /subfolder/$1 [L]
the server replies file not found
Finally I've tried
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?exemple.com$
RewriteCond %{REQUEST_URI} !^/subfolder1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder1/$1
RewriteCond %{HTTP_HOST} ^(www.)?exemple.com$
RewriteRule ^(/)?$ subfolder1/index.php [L]
but no redirection occurs
With more and more tries, I get similar results, but never the one aimed.
Any help would be awesome
Thank's in advance
ps: I'm on ubuntu 14.04.4 LTS and apache 2.4.7

htaccess redirect for www to non-www not working

This is driving me bananas. For some reason my domain will not work to redirect https-WWW to https-non-WWW. This works with every other permutation except https://www.nextoronto.com
What code do I use in the .htaccess that will allow it to work for all permutations?
Edit: This is the rewrite code now:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.nextoronto\.com [NC]
RewriteRule ^(.*)$ https://nextoronto.com/$1 [L,R=301]
It looks sound, perhaps that rule isn't being reached due to other rules in front of it?
Here is what I use that works:
# Force SSL
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://nextoronto.com/$1 [R=301,L]
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://nextoronto.com/$1 [R=301,L]
# My other rewrites for routing all requests to index.php go here...
Edit the URL and try with this code:
RewriteEngine on
rewritecond %{http_host} ^www\.example\.com [nc]
rewriteCond %{SERVER_PORT} 80
rewriterule ^(.*)$ https://example.com/$1 [r=301,nc]
Step 1
You should make .htaccess file like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1
Step 2
Go to the directory, then view mark the option file extension. Then see the .htaccess file.
When no rewrite works, no matter what you do, consider what happened to me. IIS or WAMP, on my laptop, redirected all calls to my domain (call it example.com) to localhost. So creating all the rewrites on my example.com server were to no avail because no calls to example.com ever made it out of my computer. Just do a "ping example.com" and if you get an IP of 127.0.0.1, edit your HOSTS file.

.htaccess - RewriteCond problems

There is a problem with my .htaccess file. If I type in "website.com" it redirects me correctly to "www.website.com", but if I type in "website.com/level1/level2" it redirects me to "www.website.com/index.php/level2" and gives me a 404 error.
Here is what I have in my .htacces file:
Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
AddDefaultCharset UTF-8
#Redirect from website.com to www.website.com
RewriteCond %{HTTP_HOST} ^website\.com$ [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]
Any suggestion how to solve the issue?
Thank you.
I think what's happening is that you are rewriting to index.php before you are doing the external redirect. If you switch the order of your RewriteRules, it should fix the problem. Try this:
RewriteEngine on
#Redirect from website.com to www.website.com
RewriteCond %{HTTP_HOST} ^website\.com$ [NC]
RewriteRule ^(.*)$ http://website.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
AddDefaultCharset UTF-8
This way, the external redirect to www is done first, and when the request comes back, the internal index.php is then applied, instead of the other way around.
If you have access to the server config, it is better to do hostname redirects with plain old Redirect which is designed for this exact situation. Don't resort to the complexity of mod_rewrite until you really need to.
Remove the ServerAlias for example.com in the <VirtualHost> for your main web site, and add it as a separate virtualhost instead:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
Always put rules that cause an external redirect before those rules that just cause an internal redirect. Otherwise an already internally rewritten URL would be redirected externally using the internal URL.
So in your case swap the order of your two rules putting the rule with the R flag before the other rule:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

Resources