I'm using htaccess for the first time to make pretty urls for my website html files and 1 php file. I was just wondering if I would be able to get some advice on my htaccess file set up and if how I have it set up is a good way? I'd hate for my urls to not work in some situation because of what I have written. :(
Example html file:
before: http://www.domain.com/subdomain/htmlpage.html
after: http://www.domain.com/subdomain/htmlpage/
Single php file:
before: http://www.domain.com/subdomain/phppage.php?p=1
after: http://www.domain.com/subdomain/phppage/1/
I have added in a rule to redirect index.html to index.php. I've also had to add 'base href' in the head of each file because I've used relative links.
the htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^index\.html?$ / [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
RewriteRule ^(.+)/([0-9]+)/?$ phppage.php?p=$1 [L]
This line
RewriteRule ^(.+)/([0-9]+)/?$ phppage.php?p=$1 [L]
is going to send some pages to phppage.php even though they don't look like
http://www.domain.com/subdomain/phppage/1/
because there is no mention of phppage in the first argument to RewriteRule.
try adding the following to your .htaccess file in the root of your domain
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# redirect http://www.domain.com/subdomain/htmlpage.html to http://www.domain.com/subdomain/htmlpage/
RewriteCond %{REQUEST_URI} ^(/subdomain/[^/]+)\.html$ [NC]
RewriteRule . %1/ [L,R=301]
#redirect http://www.domain.com/subdomain/phppage.php?p=1 to http://www.domain.com/subdomain/phppage/1/
RewriteCond %{REQUEST_URI} ^(/subdomain/[^/]+)\.php$ [NC]
# or alternatively if page is literally phppage uncomment below and comment above
#RewriteCond %{REQUEST_URI} ^(/subdomain/phppage)\.php$ [NC]
RewriteRule . %1/ [L,R=301]
#if url does not end with /
RewriteCond %{REQUEST_URI} ^(/subdomain/.+[^/])$ [NC]
# and its not for an existing file
RewriteCond %{REQUEST_FILENAME} !-f
# put one trailing slash
RewriteRule . %1/ [L,R=301]
#write http://www.domain.com/subdomain/htmlpage/ to htmlpage.html
RewriteCond %{REQUEST_URI} ^/subdomain/([^/]+)/$ [NC]
RewriteRule . %1.html [L]
#write http://www.domain.com/subdomain/phppage/1/ to phppage.php?p=1
RewriteCond %{REQUEST_URI} ^/subdomain/[^/]+/([0-9]+)/$ [NC]
RewriteRule . phppage.php?p=%1 [L]
Related
I'm trying to remove the last slash of my URLs.
I put this in my htaccess file :
RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
It works with files hosted in the root (or pages generated from the database), but it doesn't work with files hosted in subdirectories.
For example, I would like that this :
http://mywebsite.com/dir1/dir2/dir3/index.php
goes to that :
http://mywebsite.com/dir1/dir2/dir3
but it goes to that :
http://mywebsite.com/[homeserver]/[server]/www/dir1/dir2/dir3
What is wrong with my htaccess file ?
Edit : here is the entire htaccess file
SetEnv PHP_VER 5
SetEnv REGISTER_GLOBALS 0
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
### Remove www
RewriteCond %{HTTP_HOST} !^mywebsite\.com$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [R=301,L]
### Get the URL in pathinfo mode
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php/$1
### Redirect index.php to the root
RewriteRule ^index\.php$ http://mywebsite.com/ [R=301,L]
Try to add add below rule after RewriteEngine On and provide your full .htaccess for further analysis.
RewriteBase /
Have it like this:
SetEnv PHP_VER 5
SetEnv REGISTER_GLOBALS 0
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
### Remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=301,L]
### Redirect index.php from anywhere
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
### Get the URL in pathinfo mode
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [L]
Make sure to clear your browser cache before testing this change.
You can use below code in .htacccess file to remove slash at the end of URL.
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
I have used this and working fine.
I want create redirect in my htaccess file. From url with index.php to page without index.php, but only for one specific folder "catalog".
For example:
from /catalog/some_folder/index.php to /catalog/some_folder/ or
from /catalog/some_folder/etc/index.php to /catalog/some_folder/etc/
URL like this /catalog2/some_folder/index.php don't must redirect
I try add those lines(localhost because it was xampp):
RewriteCond %{REQUEST_URI} /catalog/+[^\.]+index.php$
RewriteRule ^index\.php$ localhost/$1 [R=301,L]
But this didn't work. How can i force this to work right?
You can use this rule in root .htaccess:
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(catalog/.+)index\.php$ /$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_URI} /index.php
RewriteRule ^(.*)index\.(php)$ https://%{SERVER_NAME}/$1 [R=301,L]
If you need to remove all "index.php" at the end of the address
To remove index.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Remember,you will need to activate mod_rewrite.
RewriteEngine On
RewriteCond %{THE_REQUEST} (?:/[^/]*)*/index\.php[?#\ ]
RewriteRule .* %{REQUEST_URI}/../ [L,R=301]
For me this is working the solution above is sending me to a folder up one level.
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>
Please read the question carefully before marking as duplicate.
We all know, that using in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
we can redirect all traffic to index.php so we can create friendly urls and have one front controller.
Although the question is connected to mod_rewrite the problem is described for Laravel.
The following .htaccess comes by default with Laravel 4 and it works fine:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If we run url mydomain.com/something and have set that route for something properly, some controller will be launched. It works fine so far.
However in Laravel 4 we will be able to reach the same route using mydomain.com/index.php/something. Probably using Laravel url creating we will have no urls with index.php in url but there is some other problem.
For example if our competition would like to make us some harm, they can simple put in Internet single links for urls to mydomain.com/index.php/something, mydomain.com/index.php/something2 and so on and search engines will see duplicate urls.
Of course if we have our custom PHP application, we can do it in PHP without a problem checking simply $_SERVER['REQUEST_URI'] and make 301 redirection. We can of course do the same in Laravel but we have to write this code in PHP each time and probably some developers could say it is bad practice to do it in PHP.
Question is simple: how can I redirect in .htaccess all urls that contain index.php to to the same url without index.php?
Example urls that should be redirected:
mydomain.com/index.php/something should be redirected to mydomain.com/something (something could be anything - can contain any characters)
mydomain.com/index.php should be redirected to mydomain.com
mydomain.com/index.php?anything should be redirected to mydomain.com (anything can contain any characters)
mydomain.com/index.phpanything should be redirected to mydomain.com anything can contain any characters)
Insert these rules just below RewriteEngine On line:
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
After spending hours I write below code for me and its 100% working
Redirect index.php to non index.php
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^index.php/(.*)$ /$1 [R=301,L]
how can I redirect in .htaccess all urls that contain index.php to to
the same url without index.php?
Add this to your .htaccess
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
For Nginx, here is the rules :
location / {
rewrite ^/(.*?)index\.php[^/] /$1? redirect;
rewrite ^/(.*?)index\.php(?:/(.*))?$ /$1$2? redirect;
}
This solved my problem to force https & remove index.php from the url in Kohan 2.3
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
RewriteRule ^(application|system) - [F,L]
RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?!localhost$|127\.0\.0\.1$)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
i have few hancoded links in my site ,some thing like this
<li><span>Bla bla bla</span></li>
My Question is ?Is it possible to remove thoose index.php using .htaccess(putting it into the root folder) by writting this in it
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
you can do It with jQuery,check every href attribute to match your pattern, and change it. It's not so pretty but anyway ,like:
$.each($('a') , function(key , value){
...
define your pattern here and change the url ...
$(value).attr('href' , newHref);
});
Yes, you remove "index.php" in ".htaccess".
In one of my yii sites i found this, first test it local...
RewriteEngine on
# redirect www to non-www (http & https)
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php