Htaccess Redirect issues in joomla project - .htaccess

I have a redirect issue.
I am trying to redirect from http://www.project.com/index.php to http://www.project.com
I tried the following, but it goes one step back to any index.php like that.
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]

To remove index.php use:
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
However, you can also use the below as an alternative (but it will require you to enable mod_rewrite for it to work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

Related

My .htaccess file fails to remove .html extensions

I'm in the process of tweaking our company website and can't seem to find the right .htaccess code to remove .html extensions from our pages (I'm not a coder, I am learning html on my own). I'm using this code to remove .html extensions and other things as well (other things work):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
#removing trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R=301,L]
#www to non
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?example\.com)$ [NC]
RewriteRule .? http://%1%{REQUEST_URI} [R=301,L]
#html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]
#index redirect
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ http://example.com/ [R=301,L]
RewriteCond %{THE_REQUEST} \.html
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
Can anyone help? This is getting really frustrating as I've tried more than 5 different variations, all of which don't work!
Thank you.
to remove .html extension use :
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
read here for more

redirecting to localhost dashboard instead of project root

I have a site in localhost - ourallnews. I want to redirect all keyword - index, index.php, index.html to site root - localhost/ourallnews/. I have applied following rule in .htaccess but it is redirected to - localhost/dashboard/. How to fix it ?
RewriteEngine on
RewriteBase /ourallnews/
RewriteRule ^(.*)index\.(php|html?)$ /$1 [R=301,NC,L]
RewriteCond %{THE_REQUEST} /category(?:\.php)?\?cat=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)$ category.php?cat=$1 [L,QSA]
If you're using a RewriteBase then just use a relative link while redirecting:
RewriteEngine on
RewriteBase /ourallnews/
RewriteRule ^(.*)index\.(?:php|html?)$ $1 [R=301,NC,NE,L]
RewriteCond %{THE_REQUEST} /category(?:\.php)?\?cat=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ category.php?cat=$1 [L,QSA]
Make sure to clear browser cache or use a new browser for your testing.

The redirection rule to the home page

What rule should I write in the .htaccess file for the redirect to work from http://site.ru/index.php/ru - to http://site.ru/ru/?
This should work.
RewriteEngine On
RewriteRule ^index.php/ru http://site.ru/ru/ [R=301,L]
Enable rewrite module in apache, then add below lines to .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
OR
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

Removing index.php from subdomain

I have the following .htaccess file in my subdomain.
My website has http://hello/forum/index.php, and would like to hide this index.php file, and make it http://hello/forum.
Whenever I use the attached .htaccess, it directs to http://hello, not http://hello/forum. Am I missing something?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) forum/$1$2 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ forum/index.php/$1 [L]
</IfModule>
Try these rules:
RewriteCond %{THE_REQUEST} ^GET\ /(forum)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /forum/index.php/$1 [L]
I was able to solve this issue by adding
DirectoryIndex index.php index.html
in .htaccess file.

How to forward from example.com/en to example.com/en/ with htaccess?

I am stuck trying to write a rewrite rule for my htaccess file which should forward from for example www.example.com/en to www.example.com/en/
I tried: RewriteRule ^en/?$ en [L]
but nothing happens. Does someone have an idea how to accomplish this?
You can use following rules in your .htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?![^/]*/).*$ %{REQUEST_URI}/ [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/$ index.php?lang=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?action=$2&lang=$1 [QSA,L]
This will:
externally redirect: /en to /en/ and internally redirect this to /index.php?lang=en
internall redirect: /fr/login to /index.php?action=login&lang=fr
Additionally: As per your comment you will need this rule to add www in domain:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Use RedirectMatch:
RedirectMatch Permanent ^/en$ /en/

Resources