htaccess redirect 301 parent url only - php

I am using clean urls on my website. I have changed the path of one of the pages. The original path is:
mywebsite.com/projects
and the new path is:
mywebsite.com/what-we-do
I tried adding a 301 redirect as follows:
Redirect 301 /projects /what-we-do
However, this works for that page, but it also means all child pages beginning /projects/xxx are getting forwarded to /what-we-do/xxx which I don't want to happen. I want the projects child pages not to redirect.

Use RedirectMatch instead of Redirect.
That uses a regular expression and allows you to specify an exact match.
RedirectMatch 301 /projects$ /what-we-do
($ being regex for "the end").

Related

htaccess redirect url to parent directory

I am getting an issue on my website. Google indexes many this kind of URLs:
http://example.com/folder/url1.php/url2.php
So, how can I redirect to my original URL, which in this case is:
http://example.com/folder/url1.php
There is another folder2 also in folder and only redirect if there is another url appended after url1.php
Frankly speaking, I am really new to this htaccess redirect, I never used this before.
So, any help will be really appreciated.
You can use RedirectMatch directive to redirect your URLs.
At the top of your htaccess file, add the following line :
RedirectMatch 301 ^/folder/([^/]+)/([^/]+)\.php$ https://example.com/folder/$1
To redirect a particular/single URI , you can use
RedirectMatch 301 ^/folder/file.php/file.php$ https://example.com/folder/file.php

Redirect issue, keeps adding string to url because of wrong rewrite

I have a link: /oldlink/ that needs to be directed to /oldlink/general but if I do this code in my htaccess there is an infinite loop because it keeps adding /general/general/general behind my URL.
Redirect 301 /oldlink/ /oldlink/general/
Is there some kind of code I can add to let it stop adding extra string to the url? Maybe a rewrite instead of a redirect?
Your redirect pattern also matches the destination url. You need to use RedirectMatch instead of Redirect directive to avoid the loop error
RedirectMatch ^/oldlink/?$ /oldlink/general/

Magento htaccess redirection issue

I was trying to redirect a particular URL and all its child URL's to a particular URL. Using 301 redirection this is working fine but the browser URL is showing the test contents also. Please see below for more details
Example: https://www.mywebsite.com/customer/account/login/referer/* needs to be redirected towards https://www.mywebsite.com/customer/account/login/register
What I tried is:
Options +FollowSymLinks
RewriteEngine on
Redirect 301 /customer/account/login/referer https://mywebsite.com/customer/account/login/register
The htaccess rule is working fine for just this URL. If I type
https://mywebsite.com/customer/account/login/referer/testing
This will be redirected towards https://mywebsite.com/customer/account/login/register/test
Please let me know how I will be able to trim the other part (URL part after register/*)from the redirected URL. What I am trying to achieve is
https://mywebsite.com/customer/account/login/referer/* [ANY URL's AFTER referer/ (Including /referer) needs to be redirected to https://mywebsite.com/customer/account/login/register].
This is expected behaviour, as stated in the documentation of Redirect
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.
To redirect all requests starting with this prefix to the given URL, you may use RedirectMatch, which uses regular expressions instead of a URL prefix
RedirectMatch ^/customer/account/login/referer https://mywebsite.com/customer/account/login/register
You don't need to use
Options +FollowSymLinks
RewriteEngine on
because this is for mod_rewrite. Redirect and RedirectMatch are provided by the mod_alias module.

Redirect old ASPX URL with commas to new PHP URL

I've searched around quite a bit for this, but haven't come across my exact issue.
I recently rebuilt an old .aspx site into WordPress, changing essentially every URL on the site. I have all the redirects in place, except there are a few URL's with commas in them that I can't seem to get to redirect.
The URLs with the commas display an Apache "Forbidden" page.
For example, I have the URL: /resourceslearn/tools/checklists,-tips,-tools/whitepapers.aspx that I need redirected to /whitepapers/
I'm not very good with regular expressions, let alone htaccess rules, so please forgive me if what I have tried is completely wrong.
I've tried the following rules:
Redirect 301 /resourceslearn/tools/checklists(.+)-tips(.+)-tools/whitepapers.aspx$ /whitepapers/
Redirect 301 /resourceslearn/tools/checklists([^/]+)-tips([^/]+)-tools/whitepapers.aspx$ /whitepapers/
Redirect 301 /resourceslearn/tools/checklists,-tips([^/]+)-tools/whitepapers.aspx$ /whitepapers/
Redirect 301 /resourceslearn/tools/checklists([^,/]+)-tips([^,/]+)-tools/whitepapers.aspx$ /whitepapers/
Can anyone give me some assistance on how I can detect the comma in the URL and redirect it to the proper page?
You can not use Regex in Redirect , try RedirectMatch instead
RedirectMatch 301 ^/resourceslearn/tools/checklists(.+)-tips(.+)-tools/whitepapers.aspx$ /whitepapers/
Reference :
https://httpd.apache.org/docs/current/mod/mod_alias.html
Redirect doesn't use regular expressions, just a URL-path. This means, you can take the path literally
Redirect /resourceslearn/tools/checklists,-tips,-tools/whitepapers.aspx /whitepapers/
When it works as it should, you may replace the status code with 301. Never test with R=301.

How to redirect just homepage to unique url with .htaccess file

I want to redirect 'packagingindustry.fraingroup.com' to 'fraingroup.com/blog' but there are several links within 'packagingindustry.fraingroup.com' that I want to redirect to just 'fraingroup.com' without the trailing slug 'blog.'
How do I uniquely just redirect the home url to include 'blog' while if any other permalinks containing any slugs (e.g. - 'packagingindustry.fraingroup.com/blog-title') redirecting to fraingroup.com (e.g. - 'fraingroup.com/blog-title')
These are the two redirect statements I'm working with:
# Redirect /index.php 'www.fraingroup.com/blog/'
# Redirect 301 / 'fraingroup.com/'
Try:
RedirectMatch 301 ^/(index\.php)?$ http://fraingroup.com/blog
The Redirect directive will also redirect everything after the match, so / redirects pretty much the entire site. However, with regex and RedirectMatch, you can limit to just an exact match.

Categories