I have a small site with about 10 pages.
So I have made it so that the URL structure is rewritten via .htaccess simply: www.domain.com/name.php becomes www.domain.com/name
The trouble now is that I added some more pages and folder and want it to recognise: www.domain.com/foldername/
However it just cannot manage it. It gives me a 404 page not found as it is searching for www.domain.com/foldername/.php
My .htaccess code is below. Any help would be greatly appreciated!
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com\
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Have your rule check for existence of corresponding .php file before adding .php extension:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
Related
I got a problem with probably my .htaccess
I use the cakephp framework (2.0) and i use .htacces to redirect a non-www to a www Using this code:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The problem is now, when somebody clicks on the link http://mysite.nl/controller/view/E1 They go to http://www.mysite.nl/index.php
instead of http://www.mysite.nl/controller/view/E1 (with www)
And this while the rewrite works on mysite.nl to www.mysite.nl
Can somebody tell me what i do wrong? i have searched for different kind of rewrite rules but with no luck.
Full .htaccess file after comment:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Ordering of rules is very important in .htaccess. In general your 301s should appear before front controller rules that catch all the requests and change the request URI.
Try this code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# add www rule
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# cakephp front controller rule
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
i have site, that has to rewrite site.ru and www.site.ru to www,site.ru/ru_RU.
I can't access any Apache config files. In htaccess:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site.ru$
RewriteRule (.*) http://www.site.ru/ru_RU [QSA]
RewriteCond %{HTTP_HOST} ^www.site.ru$
RewriteRule (.*) http://www.site.ru/ru_RU [QSA]
RewriteCond %{REQUEST_URI} ^/news
RewriteRule (.*) /news [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
It's working in firefox or chrome, but in IE i get "this page can't be displayed". Tested on IE10 and IE8 (not compatibility view) on few computers.
If i write some junk in .htacess, i get 500 error in IE. Without .htaccess site loads ok, but i need it to rewrite url. Any ideas how to fix it?
Your flags are all wrong. Modify your rules to this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.ru$ [NC]
RewriteRule ^ http://www.site.ru/ru_RU [L,R]
RewriteCond %{HTTP_HOST} ^www\.site\.ru$ [NC]
RewriteRule !ru_RU /ru_RU [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
</IfModule>
Reference: Apache mod_rewrite Introduction
I can't test it now, but here is my guess:
I think http_host ONLY contains the host part, not the uri. So you end up with an endless loop as www.site.ru is will always match again after your rewrite.
You will need another rule that checks if the uri is empty. Like this (untested):
RewriteCond %{REQUEST_URI} ^$
RewriteRule (.*) /ru_RU [QSA]
You might need another condition for the case that the uri contains a slash.
My .htaccess in a folder looks like:
RewriteEngine On
RewriteBase /profile/
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?username=$1
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/profile/$1 [R=301,L]
Basically, if you go to www.mySite.com/profile/Username, my index.php file takes 'Username' as a $_GET variable, and the URL will look clean (www.mySite.com/profile/Username)
However if you go to mySite.com/profile/username (omitting the www), the URL will look like http://www.mySite.com/profile/index.php?username=username
How can I make it so only the www is added without messing the URL up?
Thanks
Ordering of rules does matter in .htaccess.
Try this code instead:
RewriteEngine On
RewriteBase /profile/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/profile/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?username=$1 [L,QSA]
I have in my root web folder public_html/ installed my info webpage using rewrite rules (seo). In subfolder public_html/shop/ I have installed Opencart so the access to cart is http://www.mydomain.com/shop/. Each folder public_html, public_html_shop/ has own .htaccess file.
I would like to use OC1551 as multistore on several domains redirected to this folder, but also as www.mydomaincom/shop/ - not redirected: www.mydomain1.com, www.mydomain2.com and also as www.mydomain.com/shop/
*public_html/shop/.htaccess* file need to set rewritebase as /shop/ for properly working with info webpage, because use rewriting rules for www.mydomain.com/, but for www.mydoaminX.com need to be set as rewritebase / .
Can you pls someone help me with setting rewritebase in these folders to work it right? Something like dynamicaly changing rewritebase in public_html/shop/.htaccess file.
Thanks
file public_html/.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^.*$ http://www.mydomain.sk/$0 [NC,L,R=301]
RewriteRule ^(.*)/(.*)-([1-9])/(.*)/$ /index.php?category=$1&page=$2&gallery_page=$3&photo=$4 [L,QSA]
RewriteRule ^(.*)/(.*)/(.*)/$ /index.php?category=$1&page=$2&photo=$3 [L,QSA]
RewriteRule ^(.*)/(.*)-([1-9])/$ /index.php?category=$1&page=$2&gallery_page=$3 [L,QSA]
RewriteRule ^(.*)/(.*)/$ /index.php?category=$1&page=$2 [L,QSA]
RewriteRule ^(.*)/$ /index.php?category=$1 [L,QSA]
Rewriterule ^subscribe.php(.*)$ /index.php?category=overenie&addr=$1 [L,QSA]
Rewriterule ^sitemap.xml$ /webmap.php?engine=google [L,QSA]
Rewriterule ^urllist.txt$ /webmap.php?engine=yahoo [L,QSA]
file public_html/shop/.htaccess
RewriteEngine On
RewriteBase /shop/
RewriteCond %{HTTP_HOST} ^maydomain1\.com$ [NC]
RewriteRule ^.*$ http://www.mydomain1.com/$0 [NC,L,R=301]
RewriteCond %{HTTP_HOST} ^mydomain2\.com$ [NC]
RewriteRule ^.*$ http://www.mydomain2.com/$0 [NC,L,R=301]
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
I actually find on the majority of servers that you can just remove the RewriteBase completely and it's smart enough to work out what it is. I would recommend trying this and seeing if it works
I've been trying all sorts of solutions from this site and none seem to work. I'm currently hosting with hostgator. This is my current .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule mod_suphp.c>
suPHP_ConfigPath /home/user/php.ini
<Files php.ini>
order allow,deny
deny from all
</Files>
</IfModule>
This is in the root folder of my site. I have also tried adding a ? after index.php and no luck. Does anyone know why this isn't working?
This is the code you can use in your .htaccess (under DOCUMENT_ROOT) to remove index.php from URI:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
Symfony 2 has an excellent solution:
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/index.php [L]
This accomplishes the following:
RewriteBase is not necessary (useful when the site is in a subdirectory beneath the web root)
index.php is removed if present
The request is routed to the correct index.php with the full query string from the original request
Note that the line:
RewriteRule ^(.*) - [E=BASE:%1]
is responsible for setting the %{ENV:BASE} variable for later on. Refer to Apache documentation on E|env flag.
I tried this and works fine:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# 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]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
Exceptions
If your site’s system directory (/system/) has been renamed and is still accessible by URL, modify the RewriteCond line above:
RewriteCond %{REQUEST_URI} !/newdirectoryname/.* [NC]
If you are running EE from a sub-directory rather from the root of your domain (e.g. http://example.com/myeesite/ instead of http://example.com/), just remove the slash preceding index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ index.php/$1 [L]
If you are running EE from a sub-directory and it still doesn’t work after removing the slash, you may need to specify the sub-directory in your rewrite rule. For example, if your sub-folder is named testing, change:
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
To:
RewriteRule (.*?)index\.php/*(.*) testing/$1$2 [R=301,NE,L]
And change:
RewriteRule ^(.*)$ /index.php/$1 [L]
To:
RewriteRule ^(.*)$ testing/index.php/$1 [L]
If your host requires forcing query strings, try adding a question mark following index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ /index.php?/$1 [L]
To remove index.php from urls on apache2.4 you can use the following rule :
RewriteEngine on
RewriteRule ^index\.php/(.+)$ /$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !index\.php /index.php%{REQUEST_URI} [L,END]
This will change the uri
/index.php/foobar
to
/foobar
This rule will return an internal server error for lower versions of apache as they don't support the END flag. Please see the anubhava's answer above that works almost on all versions.
I've been compelled to join stack overflow.com today to comment here as the answer has solved a long term problem I've had with a Zend Framework website. I've worked on the website around 3 1/2 years and during that time I discovered that it didn't handle index.php correctly and this causes webmaster tools to see duplicate titles etc.
I decided to search again today for a solution, one of many times attempted.
This is the one (the last answer shown above) that solves my problem.
# 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]
As an example. this is what it achieves for me.
http://www.domainname.co.uk/index.php/categories/url-name.html
becomes
http://www.domainname.co.uk/categories/url-name.html
Thank you to everyone who contributed to the original question as it lead to the answer and solution above.
Extra Note: I have other rewrite commands that handles the other aspects but those on their own didn't fix the index.php and the only time this has been fixed is by using.
# 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]
I only hope it helps others whether its ExpressionEngine or Zend Framework, in the future.