Using htaccess to get rid of /index.php link? - regex

I can open my site like this:
www.mysite.com
or like this:
www.mysite.com/index.php
I want to create a htaccess rule that redirects www.mysite.com/index.php to www.mysite.com. But all my attempts have other side effects. I've tried:
Redirect index.php home.php
RewriteRule ^index.php?$ home.php [NC,L]
RewriteRule ^index.php/?$ redirect_to_home.php [NC,L]
But all of these mess up the original index.php call. So it does redirect but then the normal mysite.com link doesnt work anymore.
Any ideas?

Could you please try following.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} index\.php
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$
RewriteRule ^ %1 [R=301,L]
Explanation:
Making RewriteEngine On to make the rules work.
Mentioning condition by RewriteCond to REQUEST_FILENAME which checks if mentioned file in browser is present.
Then checking if THE_REQUEST which has complete details of request(including URL and GET/POST method) if it has index.php in it.
Now checking if REQUEST_URI is having index\.php in requested url, where saving everything before it to temp buffer memory to retrive its value later(basically its domain name).
Finally in RewriteRule to redirect complete URL with index.php to till domain name only as per requirement(R=301 is for permanent redirection on browser side).

Use this redirect rule to remove /index.php from any path:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]

Related

Redirect all urls except one in .htaccess

I am trying to redirect all URLs on my site (let's call it www.site1.com) except one in my .htaccess file. I currently have the following in my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/my/page$ [NC]
RewriteRule (.*) https://www.site2.com [R=302,L]
With the above, all requests to www.site1.com are redirected to www.site2.com, including the one that I do not want to redirect.
After some experimentation, I have found that the following works to redirect only a specific page:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/my/page$ [NC]
RewriteRule (.*) https://www.site2.com [R=302,L]
I'm not sure why the ! operator isn't working as I expect. Perhaps there is an error in my regex?
Some additional bit of information. This is a Drupal site running on a dedicated cPanel host. But I have been sure to put this new redirect rewrite rule before all the other Drupal-specific rewrite rules.
Any help would be greatly appreciated.
RewriteCond %{REQUEST_URI} !/(index.php) [NC]
RewriteRule ^(.*)$ index.php [NC,QSA,L,END]
You need to make sure you exclude the page you redirect to from you list of possible url that get redirected(to avoid the loop) and then you can add more before the RewriteRule.
RewriteCond %{REQUEST_URI} !/(index.php) [NC]
RewriteCond %{REQUEST_URI} !/(index_new.php) [NC]
RewriteRule ^(.*)$ index.php [NC,QSA,L,END]

Rewrite page & all query parameters to single URL in .htacess

Have read lots of posts on this, but still can't figure it out.
I have an old page that used lots of query parameters. I want to redirect traffic to this page to my homepage. A normal 301 redirect doesn't work when parameters are present.
I want:
/page.php
/page.php?a=1
/page.php?a=2
all to redirect to example.com/.
This code was already in my htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I added the following, immediatly below:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/page\.php$
RewriteCond %{QUERY_STRING} a=(.*)$
RewriteRule ^(.*)$ https://www.example.com/? [R=301,L]
Redirect 301 /page.php https://www.example.com/
What's happening is that only the last line is triggering. If I navigate to example.com/page.php it redirects as expected.
But if I navigate to example.com/page.php?a=1 then it redirects to example.com/?a=1 and gives me a 404 error.
Does anyone have the solution? Thanks
No need to capture query string because you want to redirect all traffic to page.php including page.php?query so , just just match against this page URI :
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/page\.php$
RewriteRule ^(.*)$ https://www.example.com/? [R=301,L]
Note: clear browser cache then test

301 redirect.htaccess on entire site EXCEPT for main domain url

I all, I've been searching a lot but I don't find an answer to my problem.
I have already made a .htaccess file that redirects all the different pages and files on my old domain (www.olddomain.com) to my new domain (www.newdomain/blog.com). I'm moving a blog to an e-commerce, that's why 301 to www.newdomain/blog.com.
However, I need to use the Change of Address Tool (Google Webmaster Tools) but I can't because www.olddomain.com must redirect to www.newdomain.com and now it is redirecting to www.newdomain/blog.com and Google don't let me change the address.
My .htaccess is working fine except for that, here it is
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule (.*)$ http:/www.newdomain.com/blog/$1 [R=301,L]
BUT, I have not found any way to make an exception to redirect just www.olddomain.com must ---> www.newdomain.com
I have tried to make that exception in the following way:
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^/$ http://www.newdomain.com/ [R=301,L]
RewriteRule (.*)$ http://www.newdomain.com/blog/$1 [R=301,L]
But nothing has worked...
Please, can you help me?
Thanks in advance
Try :
RewriteEngine On
#redirect http://oldsite.com/ to http://newsite.com
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^$ http:/www.newdomain.com/$1 [R=301,L]
#redirect http://oldsite.com/pages to http://newsite.com/blog/pages
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^(.+)$ http:/www.newdomain.com/blog/$1 [R=301,L]
Note the ^$ in pattern it matches / ie : http://example.com/ or the directory the htaccess file is in.
Clear your browser cache before testing this.

Htaccess redirect https and http and custom url

I am new to htaccess and even though I get the part to have a custom URL, I can't manage to figure out https redirect combined with having a custom URL. All the options I have found online have given me a redirect loop.
So, suppose I have a page called: mysite.com/hello.php and another one called mysite.com/bye.php.
I want mysite.com/hello.php to become mysite.com/one-hello and bye.php to become mysite.com/two-bye. This is how I have it in my htaccess now:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^one-hello$ hello.php
RewriteRule ^two-bye$ bye.php
But now I want hello.php to always show up as https but bye.php to always show up as http. How could I write all of these?
Thanks!
Have your rules like this:
RewriteRule ^one-hello$ hello.php [L]
RewriteRule ^two-bye$ bye.php [L]
RewriteCond %{HTTPS} off
RewriteRule ^hello\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R]
RewriteCond %{HTTPS} on
RewriteRule ^bye\.php$ http://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R]

htaccess: redirect old domain and all pages to a new domain

I know that there is a lot of examples on Stackoverflow but I still miss something.
I'm trying to redirect http://old.domain.com/fr/ to http://brand.new-domain.com/fr/ with the following rules, but that doesn't work:
# Enable Rewrite Engine
RewriteEngine On
RewriteBase /
# Add a trailing slash to paths without an extension
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
# Redirect domain
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^old.domain.com [OR]
RewriteCond %{HTTP_HOST} ^other-old.domain.com [NC]
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [r=301,L]
# Remove index.php
# Uses the "exclude method"
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Exclude_List_Method
# This method seems to work best for us, you might also use the include method.
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Include_List_Method
# Exclude root files
RewriteCond $1 !^(index\.php) [NC]
# Exclude EE folders
RewriteCond $1 !^(assets|ee-admin|images|templates|themes|fr|nl)/ [NC]
# Exclude user created folders
RewriteCond $1 !^(assets|css|img|js|swf|uploads)/ [NC]
# Exlude favico, robots, ipad icon
RewriteCond $1 !^(favicon\.ico|robots\.txt|pple-touch-icon\.png) [NC]
# Remove index.php
RewriteCond %{QUERY_STRING} !^(ACT=.*)$ [NC]
RewriteCond %{QUERY_STRING} !^(URL=.*)$ [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]
It correctly redirect when I call the root URL, but not when I call a page. What am I doing wrong?
Thanks in advance!
Pv
When writing mod_rewrite rules, the rules get applied in the order that they appear.
To redirect an old domain to a new domain, you'll want that rule to be first in your .htaccess or httpd.conf file — all other rules should appear after it.
If you only want to redirect a certain directory, the following rule will do so, while allowing the rest of the site to function normally:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect Only Matching Directories
RewriteCond %{REQUEST_URI} ^/(fr|fr/.*)$
RewriteRule ^(.*)$ http://brand.new-domain.com/fr/$1 [R=301,L]
</IfModule>
If you want to redirect the entire site, the following rule will do so:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^old.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^other-old.domain.com$ [NC]
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [R=301,L]
</IfModule>
If you care about letting crawlers know your content has moved and want to make the transition as seamless as possible, be sure to keep the 301 Redirect flag in the RewriteRule.
This will ensure that users and search engines are directed to the correct page.
While we're on the subject, as part of the EE 2.2 release, EllisLab now "officially" offers limited technical support for removing index.php from ExpressionEngine URLs.
Simply add or update your code to the following, making sure to consider any rules you may already have in place:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>
Try to use the following ruke as the first one:
# Redirect domain
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^old.domain.com [OR]
RewriteCond %{HTTP_HOST} ^other-old.domain.com [NC]
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [R=301,L]
Also mind the upper case R with is the short form for the lower case redirect.
Have you tried using mod_alias simple redirect instructions (a core module that you have), before trying the hacky-mod-rewrite thing?
I would do a VirtualHost with ServerName old.domain.com and in this VH I would add this rule:
Redirect /fr http://brand.new-domain.com/fr
from doc:
Then any request beginning with URL-Path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-Path will be appended to the target URL.
So get a separate VirtualHost for brand.new-domain.com (with ServerName brand.new-domain.com) and in this one do not set the Redirect Rule.
If you still want to handle the 2 domains in the same VirtualHost then you'll have to use mod-rewrite as even RedirectMatch cannot check the request domain on the query.