I have seen this asked before without an answer here: .htaccess rule to remove index.php at the end of urls
I'm having the same issue as the original poster. I have urls on my Joomla site that are showing up with index.php at the end. I want all urls ending with index.php to be redirected to the same page without the index.php at the end.
Example: www.mysite.com/forum/index.php should be redirected to www.mysite.com/forum
I have tried this code
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
But it redirects all url's with index.php at the end to my sites home page
Example www.mysite.com/forum/index.php ends up being redirected to www.mysite.com
Any help on this would be greatly appreciated!
You can use this rule as your very first rule:
RewriteCond %{THE_REQUEST} !/admin/ [NC]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
Related
I have just migrated to a WordPress website and my old site had URLs ending in .htm eg https://www.example.com/accessories.htm and I would like to 301 redirect all these URLs to root ie https://www.example.com/accessories/ - I have tried the following code in htaccess but it doesn't work
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^\.htm$ /? [R=301,NE,NC,L]
Try this rule as your topmost rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/(\S+)\.html?\s [NC]
RewriteRule ^ /%1/ [L,NE,R=301]
# remaining rules go here
Where my site is hosted, I'm using .htaccess and it has a condition to remove the www and direct to the main page without the www.
<ifModule mod_rewrite.c>
Header set X-Frame-Options DENY
RewriteEngine On
# Required to allow direct-linking of pages so they can be processed by Angular
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule (.*) http://meusite.com.br [R=301,L]
</ifModule>
The problem is this, when someone accesses an internal page with www, it falls for this check and is directed to the home, example:
If someone accesses the link: http://www.meusite.com.br/conteudo/94-vai-criar-um-site-to-your-employee-said-you-can-noble
It will direct to http://meusite.com under the condition.
What I need, is that it is directed to the following link: http://meusite.com/content/94-vai-create-a-site-to-your-employee-behavior-which-cannot-can- -fine only by removing the www from the link.
Does anyone know if this check is possible in .htaccess?
EDIT:
.htaccess is not able to translate your titles from portugese to english.
You should do redirection to normal domain with full link, and then do internal redirection with your backend (i.e. php, ruby) to proper translated link.
Use following code before your redirection, so links with conteudo will be catched here and redirected properly using backreferences:
RewriteCond %{HTTP_HOST} ^www\..* [NC]
RewriteRule ^\/conteudo\/(.*)$ http://menusite.com.br/content/$1 [R=301,L]
Soluction:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I need to redirect /index.php to the main site without index but only on the main page.
I use this code in htaccess
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
But the problem is that in Admin panel there is routing that needs index.php for pages to work properly. And this rule is deleting index.php everytime it appears on any page.
I tried adding line like this
RewriteCond %{REQUEST_URI} !^/AdminPanel/index\.php.*$
before RewriteRule but it doesn't change anything.
Hope you will help.
Remove .* from your regex:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/index\.php
RewriteRule ^index\.php$ / [R=301,L]
I have my WordPress website in a subdirectory. I used the WordPress codex directions to set this up and all worked well until client asked me to add a 301 redirect to solve an issue with some links to an old domain on the Internet. The old domain name points to the new website but does not use a permanent 301 redirect. The current domain name is Encoreco.com.
The old domain points to the same place but it is not a 301 redirect (you can see that the URL does not change at the top and for some reason this means that my javascript slideshow doesn't work). Here is what happens: encoreconstructionco.com
When I add a 301 redirect to solve this problem, I get the infinite loop errors. Here is the line of code that attempts to solve the issue with the old domain but creates the infinite loop:
RewriteRule (.*) http://encoreco.com/$1 [R=301,L]
And here is my current .htaccess: Note - I tried taking the .html rewrite and the www rewrite out and that did not solve the problem.
RewriteCond %{THE_REQUEST} \.html
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)
RewriteRule (.*) http://%1/$1 [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteRule (.*) http://encoreco.com/$1 [R=301,L]
… will redirect everything, including correct requests, so you have to get an infinite redirect.
Restrict that rule to wrong host names only:
RewriteCond %{HTTP_HOST} !encoreco\.com
RewriteRule (.*) http://encoreco.com/$1 [R=301,L]
I am currently trying to do a 301 redirect for all the pages of a site i am working on. The problem is that this url:
http://site.com/cash/
http://site.com/credit/
and
http://site.com/cash
http://site.com/credit
display the same pages
This will result in a number of duplicate URL issues and start splitting your PageRank of our SEO.
I was trying to do that on my site where all the seo points to the non-slash version
BTW I have about 90 pages that i have to change...any ideas on a good way of achieving this
How do i do a 301 redirect in the htaccess to do this on all my pages
Edit:
So to understand correctly. this will do the 301 redirect
# Remove the trailing slash
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]
But how does this exclude folder1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
and would i put that in the same htaccess file or create another htaccess file in the folder1 directory
You can add the following to your .htaccess to remove the trailing slash
# Remove the trailing slash
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]
Update
You can then choose to also add www
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com$1 [R=301]
or remove it
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
Rather than using a 301, you could just use a rel canonical link.
You could use <shudder> mod_rewrite instead, if it's for an Apache server... It's disasterously inefficient but probably the best option if what you're bothered about is SEO