htaccess to redirect to another page if the url contains '.php' - .htaccess

I'm trying to redirect any pages on my site that contain a .php at the end to th home page.
Looking around online and can't find anything on how to do this.
I've tried
RedirectMatch "^/.*.php/?$" "https://www.website.com/home/"
But it doesn't seem to work, causes an infinite loop

Related

How do I format this htaccess rule to not redirect a specific query?

I am using an htaccess rule to redirect all urls in the format of:
www.example.com/blog/the-name-of-the-post
to
www.example.com/the-name-of-the-post
So I've removed "blog" from the URL. The redirect rule below is working. However I do not want to redirect any URLs in the format of:
www.example.com/blog/page/x
So if "/page" appears after "blog", then I don't want to do the redirect. The problem is I'm also redirecting the blog pages when paginated.
RedirectMatch 301 ^/blog/([^/]+)/([^/]+)/$ https://www.example.com/$2
In sum,
www.mysite.com/blog/the-name-of-post redirects to www.example.com/the-name-of-post
www.example.com/page/1 (or page/2, page/3, etc) do not redirect
Thanks
If you don't want to redirect when page appears after blog, you can do a negative match for the word page, like so:
RedirectMatch 302 /blog/(?!page).*$ https://www.example.com/
I'm not entirely clear on what all the different possibilities are from your description so I'm just giving you an example of not matching when the word page appears in the second position of the URI.

htaccess redirect with folder name

I need some help with htaccess redirects. Out site was wrongly crawled by google and as a result there are a lot of wrong urls being shown in webmaster tool. As an example:
articles/abcd/xyz
should be redirected as
articles/abcd/
articles/abcd/xyz.php
should be redirected as
articles/abcd/
articles/abcd/xyz.html
should be redirected as
articles/abcd/
So basically I am trying to mean always redirect to articles/abcd/ for varous wrong url types that i shown above. Please could you help
You can simply use RedirectMatch from mod_alias (documentation). We assume that the part after articles does not contain any / character. We redirect with a temporary redirect to the url without the suffix. Change "temp" to "permanent" after testing that this redirect actually works as you expect it to work.
RedirectMatch temp ^(/articles/[^/]+/).+$ $1

htaccess redirect with wildcard and not recursive

I've consolidated about 20 old pages into one new page, and want to redirect web links going to those pages to the new page.
I started out listing each one in htaccess as a Redirect 301, but thought I might save processing time to do a wildcard string match instead. Unfortunately it failed, because I suspect the page I want to go to is also caught in the wildcard.
For example I want to redirect, www.mydomain.com/catalog/listname_oranges.php, listname_lemons.php, listname_figs.php etc to redirect to www.mydomain.com/catalog/listname_addons.php
So I tried this, which failed:
RedirectMatch 301 ^/catalog/listname_.*$ /catalog/listname_addons.php
How do I fix this so its not recursive?
You can use a negative lookahead in your regex:
RedirectMatch 301 ^/catalog/listname_(?!addons\.php).*$ /catalog/listname_addons.php
This way, the listname_addons.php file won't match the regex but everything else will.

How to “redirect” from main page in Prestashop to specific product page?

This question is the same one that was asked on Feb 24.
I applied the answer using my site product page which is:
Redirect 301 / http://www.papasworkshop.net/home/8-the-lifter-scrollsaw-arm-lift-assembly.html
The error message I receive is WEBPAGE NOT AVAILABLE
The URL line contains:
http://www.papasworkshop.net/home/8-the-lifter-scrollsaw-arm-lift-assembly.htmlhome/8-the-lifter-scrollsaw-arm-lift-assembly.html (and all of this text- except for the web address - repeats many many times on the URL line.
Try using RedirectMatch instead because Redirect links two different path nodes together. Meaning, / gets symbolically linked to http://www.papasworkshop.net/home/8-the-lifter-scrollsaw-arm-lift-assembly.html
. Thus anything after the / also appears at the end of http://www.papasworkshop.net/home/8-the-lifter-scrollsaw-arm-lift-assembly.html.
Incidentally, that means you're browser will continue to loop and constantly get redirected by the server. So try:
RedirectMatch 301 ^/$ http://www.papasworkshop.net/home/8-the-lifter-scrollsaw-arm-lift-assembly.html
You need to create a .htaccess file in the /home subdirectory with the following line:
RewriteEngine On

htacces redirection if the url doesn't have .php extension

I wish to redirect all page requests if
(1) Current URL doesn't have .php extension
(2) Current Page is not the Site Home (ie www.site.com )
Example:
http://site.com/robin to profile.php?screen_name=robin
Can someone suggests the htaccess to accomplish this?
This site should give some hints:
http://www.webweaver.nu/html-tips/web-redirection.shtml
an excerpt from there:
Changed file extension?
This example is perfect if you've
decided to switch to .php from .html
pages. It will look for any .html page
and redirect it to .php (ie
http://www.example.com/yourpage.html
and redirect it to
http://www.example.com/yourpage.php).
Now, be careful with this, it does
mean any html page. I did this on one
of my sites and had totally forgotten
I had an iframe with .html content on
some pages... I didn't notice for
weeks that it was broken :S. So learn
from my mistake ;-) check, double
check, then check again.
RedirectMatch 301 (.*)\.html$ http://www.example.com$1.php
You can put arbitrary regular expressions as parameter to Redirectmatch, so you need to add a regular expression which matches '".php" not last of line'. I am only a neophyte of RegExes myelf, so i do not provide an example yet. Check for a later update -have to do some experimating.

Resources