Redirecting urls ending in index.php to clean url - redirect

I have a client with a strange set up. In analytics we are seeing urls getting pageviews that technically dont exist.
Example: website.org/questions/search/blog/2016/07/blog-post-name/index.php
The current ht access file looks like this but all that does is redirect these blog urls to the home page.
# Redirect index.php Requests
# ------------------------------
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,L]
Is there an htaccess code to create redirect:
Example: website.org/questions/search/blog/2016/07/blog-post-name/index.php
to: website.org/questions/search/blog/2016/07/blog-post-name
Thanks

Related

Backlink from Instagram throws 404, Need it to correctly redirect to root url instead

My self-coded portfolio website is giving me problems when used as an Instagram profile link (let's say www.myurl.com): the Instagram browser displays a 404 error when clicking the profile link. Clicking "Back to Index" on the 404 page shows the website as it should. After checking the URL, I see that Instagram automatically adds a random string to the domain (like www.myurl.com/?fbclid=PAAaa-Vm9q.../) and I assume my website doesn't know how to redirect this. The issue does not occur on a desktop computer, only mobile.
I'm guessing this random string is an SEO tool, which I woefully do not know how to use.
But I need the website link on IG to go through to my homepage without throwing the 404 error, at a minimum. Is there some kind of code / html document / web hosting / dns setting I need to manipulate to get this to work?
I have a feeling this is an issue caused by DNS settings on Cloudflare since I recently set up a proxy with them to use their global SSL certificate.
Some notes about my website in case they are helpful:
hosting on GoDaddy, recently proxied through CloudFlare for SSL certificate
I have the main domain (www.myurl.com) automatically redirect to a subdomain (sub.myurl.com). This subdomain is actually stored in a folder like www.myurl.com/sub/ .
Website is a CMS built on PHP 5
I tried URL shortener services as a stopgap measure. The only one that solved the problem was shorturl.at, but tiny.url and bit.ly still had the 404 issue. I have no idea why.
I looked at the htaccess document in the subdomain root folder, the contents are below. I think the issue may be that the "base" needs to be "rewritten" to the subfolder (www.myurl.com/sub/) but I'm not sure how to correctly change this code:
RewriteEngine on
# Some hosts require a rewritebase rule, if so, uncomment the RewriteBase line below. If you are running from a subdirectory, your rewritebase should match the name of the path to where stacey is stored.
# ie. if in a folder named 'stacey', RewriteBase /stacey
#RewriteBase /
ErrorDocument 404 /404.html
# Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]
# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.)
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ([^/]+)$ $1/ [L]
# Rewrite any calls to /* or /app to the index.php file
RewriteCond %{REQUEST_URI} /app/$
RewriteRule ^app/ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?$1 [L]
# Rewrite any file calls to the public directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(.+)$ public/$1 [L]
I'm very much a beginner with webhosting / coding / etc so will appreciate an idiot-friendly explanation!! Thanks in advance.

Redirect rule to allow sub.domain.com to only serve images

I need help with a redirect rule.
I created a subdomain sub.abc.com and set it's public_html to that of abc.com.
This means public_html is now accessible either by abc.com or by sub.abc.com .
I want to limit this so that sub.abc.com only provides access to images in wp-content/uploads any other requests to sub.abc.com should be redirected to abc.com/url/url .
Here is what i have tried creating
RewriteCond %{HTTP_HOST} ^cdn.example.com
RewriteCond %{REQUEST_URI} !^/wp-content/uploads/$1
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
so if the user is accessing cdn.example.com/wp-content/uploads/1/1/file.jpg then there is no redirect, but if they are trying to access cdn.example.com/post/post or cdn.example.com/anything-else then it will redirect to example.com/post/post or example.com/anything-else .
The following rule should work for you. Remember to put it at the of your htaccess or server.config file before others rules otherwise these rules may override it.
RewriteEngine on
#serve only images on abc.example.com
#redirect to main domain if request is not for images
######################
#if the host is "sub.example.com"
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
#and the request is not for images and /wp-content/uploads
RewriteCond %{REQUEST_URI} !/wp-content/uploads/?$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpg|png|gif|jpeg)$ [NC]
#redirect the request to main domain
RewriteRule ^ https://example.com%{REQUEST_URI} [L,R=302]
This will redirect your sub.example.com with request string to example.com if the subdomain is used for requests other then images .
Replace sub.example.com and example.com with your real domain name in the RewriteCond and RewriteRule above.
If this works and you are happy with the redirect then change R=302 to R=301 to make the redirection permanent.

Redirecting full URL including directories

I'm migrating to a new website.
I want to be able to redirect everything from the old website to the new website in the following structure.
Example 1:
http://dev.website-old.com/abc/123
to
http://dev.website-new.com/abc/123
Example 2:
http://dev.website-old.com/abc/123/xyz
to
http://dev.website-new.com/abc/123/xyz
How do I accomplish this?
This rewrite redirect all requests (1:1) from dev.website-old.com to dev.website-new.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?dev\.website-old\.com [NC]
RewriteRule (.*) http://dev.website-new.com/$1 [R=301,L]

Why are my forms not working after .htaccess URL rewrite?

I copied the below code from some other posts. It successfully helped me to stripped the .php extension from the URL. However, after implementing this, my forms are no longer working. I have forms that are posting like <form method='post' action='login.php' .... Now it is not sending the data to the location when I submit the form. I realize that if I change the action='login.php' to action='login', the forms will work again. But there are a number of pages that contain forms posting to different locations in my website, and I do not want to change so many of them manually. So I wouold like to know why the data are not posting as expected and how can I solve the problem. Thank you!
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://localhost/domainname/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_FILENAME} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://localhost/domainname/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://localhost/domainname/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
Add this rule on top before other rules to skip all POST requests from rewrite:
RewriteCond %{REQUEST_METHOD} =POST
RewriteRule ^ - [L]
I want to redirect user_login.php to seo friendly url like /user-login with post form data and this worked for me.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/user_login\.php [NC]
RewriteRule ^ user-login [QSA,R=301]
RewriteRule ^user-login$ user_login.php [QSA,L]
In view file
<form action="<?php $siteurl;?>/user-login" method="post" id="user_login">

How do I use .htaccess to force www. while using Zend Framework

I want to force a www. prefix on my website by using a .htaccess 301 redirect. I am currently trying:
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
Which normally works, but I am using Zend Framework which causes all requests to be redirected back to http://www.mysite.com/index.php regardless of the initial request.
For example...
http://mysite.com/blog,
http://mysite.com/contact,
http://mysite.com/blog/this-is-my-article,
Will all be redirected to http://www.mysite.com/index.php
However, if I initially request a specific file, such as...
http://mysite.com/some-file.htm
The redirect works properly, redirecting to http://www.mysite.com/some-file.htm
In first time, don't forget to enable the rewriting ("RewriteEngine on").
The last line is important if you use Zend Framework.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteRule !\.(pdf|php|js|ico|txt|gif|jpg|png|css|rss|zip|tar\.gz)$ index.php
Now the url...
http://mysite.com/some-file.htm
... redirect to http://www.mysite.com/some-file.htm but use the index.php
Don't forget to ignore sub-domains when re-directing to www.
http://www.theblogaholic.com/2011/01/16/force-www-using-htaccess-except-for-subdomains/