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'].
Related
I am trying to redirect my page from /index.php to /index?action=login but my .htaccess file is not working. I checked with
a2enmod rewrite
it returned
Module rewrite already enabled
I wrote a simple expression to redirect from index.php to index. But this is even not working
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index/$ /index.php [R=301,L]
I can't understand the mistake I was doing thanks in advance
To redirect /index.php to /index.php?action=login you can use the following rule :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php$ /index.php?action=login [L,R=301]
The RewriteCondition RewriteCond %{QUERY_STRING} ^$ checks to see if there is no query string in the old uri . Otherwise without the condition you will get a redirect loop error since both the old and the destination uri are identical.
RewriteEngine On
RewriteRule ^([^/]+)/? index.php?action=$1 [L,QSA]
With this rule you redirect from /login to /index.php?action=login
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.
I would like to redirect with htaccess to a certain folder 'setup' if it exists.
Else it must do the stock standard redirect as it was :
RewriteEngine on
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://%{HTTP_HOST}/$1 [L]
RewriteBase /
RewriteRule ^$ /site [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /api/index.php [L]
Now my htaccess-Fu are not nearly where it supposed to be. I read the posts I could find on SO but none of them helped for my specific case.
Then I have a sub-question: Is it bad practice to have this permanently in your htaccess file? Seeing as it would only exist the first time you setup the site?
I often see people doing this via PHP, but doing it via htaccess does seem like a great alternative!
So what you're looking for is something like:
RewriteEngine on
# The part your looking for: If setup is present and you're not in it redirect there!
# If the setup directory exists in the document root...
RewriteCond %{DOCUMENT_ROOT}/setup -d
# ...and you're not in setup..
RewriteCond %{REQUEST_URI} !(setup) [NC]
# ...redirect to setup - with a 302 (temporary redirect) to make sure the browser won't cache the redirect.
RewriteRule ^(.*) /setup [L,redirect=302]
# the rest of your htaccess should go here... for instance:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
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 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]