.htaccess code making a wrong url redirect - php

This is my .htaccess code
#RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{HTTP_HOST} !hashstar\.com$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
#over here we have set the default root directory now request will be directly made from this directory
RewriteCond %{HTTP_HOST} ^hashstar.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.hashstar.com$
RewriteCond %{REQUEST_URI} !app/
RewriteRule (.*) /app/$1 [L]
#redirect all requests except only POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:php?)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
#we are setting here the default file to load while the URL is called followed by fallback files
DirectoryIndex index.php timer/index.php
My root directory is set to folder app
In app folder, I have 2 folders
1.) Manage
2.) Timer
To access manage folder I have to type in URL
http://www.example.com/manage/
To access timer folder I have to type in URL
http://www.example.com/timer/
if I use these Url by adding a slash in URL (/)
the URL loads perfectly
but if the don't add the (/) in URL then it makes a redirection through the root directory
URL should look like => http://www.example.com/manage
It becomes like => http://www.example.com/app/manage/
What's the reason behind it & how to fix this problem?
Any helps appreciated. Thanks in advance.

Reason of this redirect is that rewritten URI is a real directory without trailing slash. When this happens, Apache's mod_dir module acts on it by redirecting to a URI with a trailing slash.
To prevent use this .htaccess:
#we are setting here the default file to load while the URL is called followed by fallback files
DirectoryIndex index.php
#RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{HTTP_HOST} !hashstar\.com$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
#redirect all requests except only POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:php?)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
# adds a trailing directory if rewritten URI is a direcory
RewriteCond %{DOCUMENT_ROOT}/app/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L]
#over here we have set the default root directory now request will be directly made from this directory
RewriteCond %{HTTP_HOST} ^(?:www\.)?hashstar.com$ [NC]
RewriteCond %{REQUEST_URI} !/app/ [NC]
RewriteRule (.*) /app/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

At a glance (I didn't try to reproduce these rules in my Apache2 server), it would seem that this is the culprit:
RewriteRule (.*) /app/$1 [L]
the Regular Expression (.*) will catch Any character greedily. Your Rewrite Rule is simply adding /app/ between the domain and the script.
Find and group <all non-null characters>,
and transform it to /app/<all non-null characters>
The URL www.example.com/myscript.php would be rewritten as www.example.com/app/myscript.php
EDIT
so what code edit do u suggest? #SsJVasto
You could simply exclude / from the greedy rule:
RewriteRule (.*)/ $1 [L]
This way, you are storing the result of anything ending with a /, but without said / into $1. Since the / isn't inside the group, it will be forgotten.
If you want to handle repetition (like http://www.example.com/myscript.php//), you can add a + sign to handle one or more occurrences:
RewriteRule (.*)/+ $1 [L]

Related

How to Force domain to with www - htaccess

I'm using this htaccess to force all request with www. but my resources don't loaded:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^shadyab.com [NC]
RewriteRule ^(.*)$ http://www.shadyab.com/$1 [L,R=301,NC]
RewriteRule ^ /index.html [L]
this is my site:
http://www.shadyab.com/
for example:
http://www.shadyab.com/assets/plugin/slider/css/owl.carousel.min.css
Your second rule rewrites everything to index.html, including all your css
If you really want to rewrite every request to index.html, but still want your resources, you can exclude them using a condition, otherwise remove the rule.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^shadyab.com [NC]
RewriteRule ^(.*)$ http://www.shadyab.com/$1 [L,R=301,NC]
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpe?g|gif)$ [NC]
RewriteRule ^ /index.html [L]

Htaccess redirect url path different path

I want to masking url with htaccess. I tried a few method but didn't work.
I used this code
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ index.php
RewriteCond %{HTTP_HOST} ^em.example.com/lib/view/map$ [NC]
RewriteRule ^pathurl/(.*)$ /em.example.com/map/$1 [R=301,NC,L]
Url is
https://em.example.com/lib/view/map.php
I want to redirect to
https://em.example.com/map
Try
RewriteRule ^/lib/view/(.*)$ $1 [NC]
RewriteRule ^(.*).php$ $1 [R=301,NC,L]
First rule removes lib/view/ , Second rule removes .php

htaccess by adding index.php in the root of the site

This is my current htaccess:
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !^/1EE772B8EBDDECBA378B575830E7E1B3\.txt$
RewriteCond $1 !^(index\.php|assets|wmt|google1f91ad48697ced36\.html|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTP_HOST} ^domain.com.br$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
Options -Indexes
In the first block lines it redirects the http to https;
In the second block it allows some direct access and removes the index.php
In the third block it adds the www if it does not exist.
The ultimate goal is to have urls like:
https://www.domain.com.br ou
https://www.domain.com.br/foo/bar
The problem is that when the user enters the address
Domain.com.br (without www and without the http, only in the root of the site) it redirects to:
https://www.domain.com.br/index.php
Only at the root of the site does it add this index.php
Does anyone know where I'm going wrong?
I accept htaccess optimization suggestions as well
You can use these refactored rules:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_URI} !^/1EE772B8EBDDECBA378B575830E7E1B3\.txt$
RewriteCond $0 !^(index\.php|assets|wmt|google1f91ad48697ced36\.html|robots\.txt) [NC]
RewriteRule .* index.php/$0 [L]
Test it after completely clearing your browser cache.

htaccess remove index.php and add www to url in expressionengine

i made a website by expressionengine, in the htaccess i need to remove the index.php and also add www to none www url's.
here is my current .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Add www to url
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)/index\.php/*(.*) /$1$2 [R=301,NE,L]
</IfModule>
but the above code has some problems, it works when the url has the www itself and there is no index.php anywhere and everything is fine, but when i remove the www from the url to test the "add www" part it does not work any more and the url damages lik this:
http://www.example.com/index.php?/segment1/segment2/
an index.php comes up. i don know what to do, any help will be appriciated
Try this..
RewriteEngine On
#Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
it will remove index.php from the URL and add www as a prefix.
The www redirect probably doesn't work because there's no wildcard, keep it simple and specify your domain:
# Redirect to www
# -------------------
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
I expect you have the removal and addition of index.php the wrong way around, I redirect it first if specified in the URL, then add it in hidden:
# Redirect specific index.php requests in URL
# ------------------------------
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Also try with and without the ? on this line, depending on the server setup, this might not be needed:
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
or
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Codeigniter remove index.php and prefix the URL with www

I want to remove the default index.php that auto comes with Codeigniter.
I've been able to remove that with this code
RewriteRule ^(.*)$ index.php/$1 [L]
example.com/index.php/blog can now be accessed by example.com/blog
I later wanted to prefix the URL with a www i.e example.com/blog should redirect to www.example.com/blog with these rewrite rules
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]
After adding this code above to the end of my .htaccess file, it began to misbehave.
If I enter www.example.com/blog into a URL bar it works fine but if I enter example.com/blog it redirects to www.example.com/index.php/blog
What I'm I doing wrong?
I want example.com/blog to redirect to www.example.com/blog
Note: I am using the Codeigniter framework.
Added: This code is just on top of the previous ones I have up. Maybe this is it's problem please HELP!!!
RewriteCond $1 !^{index\.php|[assests/images/themes/fonts/style/scripts/js/install]|robot\.txt|favicon\.ico}
Try this:
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ example.com/$1 [L,R=301]
After much tricks and following the post given by #Tpojka I was able to come up with this
RewriteEngine On
RewriteBase /
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# no www -> www
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
This guy was my problem
RewriteCond $1 !^{index\.php|[assests/images/themes/fonts/style/scripts/js/install]|robot\.txt|favicon\.ico}
I would need help on how to exclude folders and some files like robot.txt file like I tried in the malfunctioning line.
This is the complete .htaccess file which I use personally:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

Categories