Index Page Showed on All Pages In Directory - .htaccess

We are using a CMS called ExpressionEngine.
We have template/folder called Contact - that has and index file. We only have the index page for contact because we have no deeper URLs that we need for Contact.
However if you add anything to the end of the normal contact pages URL it loads the same content at the contact page but isn't redirects and keeps the random URL.
Example:
This is the correct URL and displays the index content:
http://www.example.com/contact/
These are incorrect URLs (that still work - everything works) and it displays the index content:
http://www.example.com/contact/kjhfd/
http://www.example.com/contact/kjhfd/kljhdf/
http://www.example.com/contact/f/
We want that to stop and 301 redirect to http://www.example.com/contact/.
I have tried RedirectMatch with htaccess - I've tried 301 Redirect in htaccess and I've tried a funky PHP if redirect - none of which have worked so far
We have no PHP redirects. We have many htaccess redirects, please see below:
RewriteEngine On
#Trailing backslash
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* [QSA,L]

A quick and easy fix is to check for the segment_3 in the URL,if it exist then redirect to contact index page.You can use {redirect} tag.
{if segment_3}
{redirect ="/contact"}
{/if}

You can use the following handler in htaccess :
RedirectMatch ^/contact/.+$ /contact
This will redirect a url with trailing path(s) to the correct location
/contact/foo
to
/contact

Related

htaccess RewriteRule and Redirect 301 interfering with one another

My .htaccess file includes this, which allows the URL structure in my ExpressionEngine site to function:
RewriteRule ^(.*)$ /index.php?/$1 [L]
However, it is interfering with what I'm trying to do here, redirecting an old page:
Redirect 301 /old-url/ /
This is what I see in my browser address after I load the /old-url/ address:
https://www.domain.test/?/old-url/
If I take out the RewriteRule, my Redirect 301 works, but all the navigation in the site breaks. Is there some way for me to make an exception where I want to redirect old URLs? I have inherited someone else's work in this case.
Edit: here is my entire .htaccess file content:
RewriteEngine On
Redirect 301 /in-loving-memory-of-our-partners /
Redirect 301 /in-loving-memory-of-our-partners/ /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^sitemap\.xml$ /sitemap/xml [NC,L]
RewriteCond $1 !^(assets|emergency|portalimages|images|system|themes|test|AUL|swfs|favicon\.ico|robots\.txt|index\.php) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteRule ^(.well-known) - [L]

Redirecting a page to another while reporting 301

I have a page at example.com/news.php. This page is already indexed by Google. I would like to ditch this page and instead use example.com/news.
So essentially I need to report 301 on news.php and also redirect any request to /news to news.php.
When I am using the below in .htaccess I am getting a redirect loop:
Redirect 301 /news.php http://example.com/news
RewriteRule ^news$ news.php [L]
How would I go about doing the desired?
Try :
RewriteEngine on
RewriteCond %{THE_REQUEST} /news\.php [NC]
RewriteRule ^ /news [L,R=301]
RewriteRule ^news/?$ news.php [L]
Edit as per your comment :
To redirect any php file , you can use :
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/?$ $1.php [L]

.htaccess not redirecting from old url to new url

i have developed a website using codeigniter. Previously the site had a long URL structure so i have made them shorter in the new website.
Although i used the redirect directive in htaccess it gives me a 404 error. I have removed all the old controllers and functions.
below is a few lines from htaccess (there are many urls redirecting to new ones)
RewriteEngine On
# Redirect non-www urls to www
RewriteCond %{HTTP_HOST} ^mysite\.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
#RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Redirect 301 /payments/charity_and_donations/paid http://www.mysite.com/charity
Redirect 301 /outwards/office_furniture/damaged http://www.mysite.com/office_assets
Redirect 301 /funding/business_and_person/inward http://www.mysite.com/funds
can someone tell me why it is not redirecting from the old to the new and what am i doing wrong?
You need to have your redirecting happen before you route URIs to /index.php. Also, since Redirect is part of mod_alias and your other redirect/routing rule is mod_rewrite, the URI is being processed twice when it's not supposed to. You should just use mod_rewrite and add the rules to the beginning:
RewriteEngine On
# Redirect non-www urls to www
RewriteCond %{HTTP_HOST} ^mysite\.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
RewriteRule ^payments/charity_and_donations/paid http://www.mysite.com/charity [R=301,L]
RewriteRule ^outwards/office_furniture/damaged http://www.mysite.com/office_assets [R=301,L]
RewriteRule ^funding/business_and_person/inward http://www.mysite.com/funds [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Try the below code, this works perfectly for me
RewriteCond %{HTTP_HOST} !^www\.yoursite\.com$ [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

custom 301 redirects from old joomla (mambo) website to new drupal 7 website using .htaccess

The past couple of hours I am trying to create custom redirects from an old mambo website to new drupal 7 website with the .htaccess file that exists in my drupal's root.
What I want to do is...
301 Redirect
http://mysite.com/index.php?option=com_content&task=blogsection&id=11&Itemid=54
to
http://mysite.com/this-is-the-new-page
This is my .htaccess file...
RewriteEngine on
RewriteRule ^index.php?option=com_content&task=blogsection&id=11&Itemid=54$ http://mysite.com/this-is-the-new-page [R=301,L]
RewriteRule "(^|/)\." - [F]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
I am sure that it has something to do with this line...
RewriteRule ^ index.php [L]
But I don't get it!
You see if I use this...
RewriteRule ^option=com_content&task=blogsection&id=11&Itemid=54$ http://mysite.com/this-is-the-new-page [R=301,L]
instead of this...
RewriteRule ^index.php?option=com_content&task=blogsection&id=11&Itemid=54$ http://mysite.com/this-is-the-new-page [R=301,L]
and test it with firefox and LiveHTTP Headers addon it works!
Any suggestions?!
Thanks!
The query string is not part of the URL path pattern. If you want to base a rule on the query string, you must do so in a RewriteCond
RewriteEngine on
RewriteCond %{QUERY_STRING} option=com_content&task=blogsection&id=11&Itemid=54
RewriteRule ^index.php$ /this-is-the-new-page? [R,L]

.htaccess redirect from 1 url (no matter what file/folder) to another url

I have a website http://rochesterwaterskishow.com which they've recently changed their name so they want to update their url to http://skidox.com. I'm trying to redirect any page from rochesterwaterskishow.com to skidox.com/site/index.
I have this line of code which redirects http://rochesterwaterskishow.com to http://skidox.com, but if I go to something like http://rochesterwaterskishow.com/test, it doesn't redirect to http://skidox.com.
RewriteRule ^$ http://skidox.com/site/index [R=301,L]
How can I make it a catch all so anything rochesterwaterskishow.com/* gets redirected to skidox.com/site/index?
UPDATE: Full .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^$ http://skidox.com/site/index [R=301,L]
That's because the search pattern ^$ will only match a URI path of "/". You need to pick up the request in a match variable, for example:
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^.* http://skidox.com/site/index/$0 [R=301,L]
I am assuming that you are using SEO optimised-style URIs for the new site. If you want to simply redirect everything to the index page without any context, then you still need a pattern that matches:
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^ http://skidox.com/site/index [R=301,L]
Update following post of full htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} rochesterwaterskishow
RewriteRule ^.* http://skidox.com/$0 [R=301,L]
RewriteCond $0 ^(index\.php$|robots\.txt$|resources)
RewriteRule ^.* - [S=1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteRule ^$ http://skidox.com/site/index/$1 [R=301,L]

Resources