Redirect /index.php - wordpress - php

I want that if someone got link to my site, lets say: www.bla.com/index.php , he will redirect to www.bla.com. or in another words - remove the index.php.
Here is my .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^bla\.com$ [NC]
RewriteRule ^(.*)$ http://www.bla.com/$1 [L,R=301]
Either if someone got link: www.bla.com/camera/index.php - it will redirect to www.bla.com/camera/.
For now, the site itself works great, without index.php, but i want to remove the index.php if someone came from extern link.

This post is tagged with WordPress. All of what you're looking for and doing (canonical domain, canonical url without index.php) has been built into WordPress at least since WP 3.0 when permalinks are turned on... Look into canonical redirects -- you'll find the logic in there.

try like that in
.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^bla\.com$ [NC]
RewriteRule ^(.*)$ http://www.bla.com/$1 [L,R=301]
RewriteRule ^index.php/(.*)$ /$1 [R=302,L]
RewriteRule ^(.*)$ index.php?/$1 [L]

Try this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

I believe the OP wants to prevent using index.php even when external links are involved. After following the advice from the other question. Try this.
RewriteRule ^(.*)/index.php$ http://www.bla.com/$1/ [L,R=301]

Related

301 Redirection is appending ?_url

I'm trying to setup 301 redirections for a website that's build with Phalcon Framework.
The Problem
My redirection is working fine, but it's appending a ?_url= with the old url after the redirection.
Default .htaccess
Here's what comes with the Phalcon Framework in the .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
Example
When a user is going on http://example.net/fr/le-circuit/a-propos, it's redirecting to http://example.net/fr/circuit?_url=/fr/le-circuit/a-propos
As you can see, the ?_url= is the extra that I'm trying to remove.
My .htaccess
Here's my .htaccess without the redirection:
AddDefaultCharset UTF-8
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirection for old website links
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^$ /fr/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ /en/ [L,R=301]
RewriteRule ^$ /fr/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
</IfModule>
Attempt #1
I've tried the following Redirect directive before and after the default rewrite rules that came with Phalcon.
Redirect 301 /fr/le-circuit/a-propos /fr/circuit
Attempt #2
I thought my first attempt wasn't strict enough so I decided to use regex and RedirectMatch:
RedirectMatch 301 ^/fr/le-circuit/a-propos/?$ /fr/circuit
Just like before, I've tried it before and after the rules that came with Phalcon and it's appending the old url after the redirection.
Attempt #3
I've tried to hardcode the url in the redirection to see if the ?_url= would still append and ... yes it does.
RewriteMatch 301 ^/fr/le-circuit/a-propos/?$ http://example.net/fr/circuit
Does anyone know the problem or have an idea why it's appending the old url after the redirection?
Keep redirect rules before your other rules:
RewriteEngine On
# Redirection for old website links
RewriteRule ^fr/le-circuit/a-propos/?$ http://example.net/fr/circuit [L,NC,R=301]
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^$ /fr/ [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ /en/ [L,R=301]
RewriteRule ^$ /fr/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
Make sure to clear your browser cache while testing this change.
Make sure you clear your cache when working with 301 permanent redirects. Always debug them as 302 temporary redirects. Make sure your cache clearing prevents the redirection from happening when you comment out the redirects in your .htaccess file. Once you've done that, you can try the following suggestion:
Ditch the "_url" altogether by using REQUEST_URI from your bootstrap instead:
Use RewriteRule .* index.php [L] instead of: RewriteRule ^(.*)$ index.php?_url=/$1 [QSA,L]
Then use this line in your index.php bootstrap:
echo $application->handle(strtok($_SERVER["REQUEST_URI"],'?'))->getContent();
Rather than echo $application->handle()->getContent(); which defaults to handle $_GET['_url'].

(Wordpress) .htaccess Selective Redirect

I want to redirect the home of my old site to the home of my new site, while I want every subpage to redirect to the page search of the new site.
Briefly:
www.oldsite.it --> http://newsite.it
and
www.oldsite.it/whatever --> http://newsite.it/search?q=whatever
I want the conditions to live both and strictly.
I achieved to let them work but separately.
Here's my code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} oldsite\.it/$
RewriteRule ^(.*)$ http://newsite.it/search?q=$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
How can I make them work?
Thanks
You need 2 conditional rules here to redirect the old homepage and old urls to the new location ,try :
RewriteEngine On
RewriteBase /
#1)redirect "oldsite.it" to "newsite.it#
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.it$
RewriteRule ^$ http://newsite.it [NC,L,R]
#2)redirect "oldsite.it/foobar" to "newsite.it/search?q=foobar#
RewriteCond %{HTTP_HOST} ^(www\.)?oldsite\.it$
RewriteRule ^(.+)$ http://newsite.it/search?q=$1 [QSA,NC,R,L]
Clear your browser's cache before testing these rules.
To make the redirect permanent ,change R to R=301 in both rules when you are sure the rules are working.

Redirect Loop error when wordpress is installed to a folder

I have a website built on PHP on the base domain. I have installed a wordpress blog on /blog. For SEO purposes, I have done canonical redirection on the base domain with the code
RewriteCond %{HTTP_HOST} ^domainname\.com
RewriteRule ^(.*)$ http://www.domainname\.com$1 [L,R=301]
However this creates a redirect loop when I try to access www.domainname.com/blog. The base website works as expected without any issues. The htaccess file associated with wordpress is
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
Since canonical redirection is essential for SEO, I need to have that code for the base domain. How can the blog issue be rectified while still making the canonical redirections work? Any insights will be helpful. Thanks in advance.
Change your configuration to this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

old .aspx URL redirect to new WordPress URL

A client of mine had me redesign his old asp.net website and I have developed it using WordPress. Same domain name. It is finished and live.
During this transition, I had to re-create nearly 250 landing pages and name them using the same url structure that they were named previously, primarily for SEO purposes and the fact that these pages and links are numerous places on the web.
Although, I suspected this would require a RewriteRule in the .htaccess file, this hasn't been a seamless transition for us at all.
I need to have to redirect:
http://website.com/pages/about-us.aspx to http://website.com/pages/about-us/
Currently, my .htaccess file includes the following:
AddHandler php-stable .php
# 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]
RewriteCond %{REQUEST_FILENAME}\.aspx -f
RewriteCond %{HTTP_HOST} ^mysite.com$ [NC]
RewriteRule ^/?(.*)$ mysite.com/$1 [R=301,L]
</IfModule>
# END WordPress
This is having no affect and I still get a 404 error when going to these .aspx pages.
Any help would be greatly appreciated.
Thanks!
Changing
RewriteCond %{REQUEST_FILENAME}\.aspx -f
RewriteCond %{HTTP_HOST} ^mysite.com$ [NC]
RewriteRule ^/?(.*)$ mysite.com/$1 [R=301,L]
to:
RewriteRule ^(.*).aspx$ /$1 [R=301,L]
should work.

Redirect from .htaccess does not work

I have a website and i want to make it always show as www.mysite .com i put that code in .htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
now the url shows as www but the website is not shown the browser said "This webpage has a redirect loop"
Note: .htcaccess do not have any code but this few lines i wrote
does i did anything wrong or i miss something??? please help
Edit:
before I add the above code the .htaccess had the following code but not direct to www i remove it :
# 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>
RewriteCond %{QUERY_STRING} ^m=1$
RewriteRule (.*) $1? [R=permanent]
# END WordPress
could that be updated to solve my problem?
if you are doing it at windows server it will not work if it is window you have to create web.config file n for linux it should be .htaccess
Ok so Wordpress is also there. Make sure to do these 2 things:
Put your 301 rule above WP's rules i.e. just below RewriteBase line
Update WP settings to have your site address with www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
or
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Categories