htaccess redirect url to parent directory - php

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

Related

htaccess redirect 301 parent url only

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").

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.

wordpress url rewriting issue for one page or post

I need help on wordpress Rewirite url.
http://www.example.com/this-is-a-test-title/93/
http://www.example.com/this-is-a-test-title2/93/
http://www.example.com/this-is-a-test-title3/93/
All these urls should point to
http://www.example.com/?p=93
any string after example.com/%%%%%%/93 should needs to point to
http://www.example.com/?p=93
Its should works only particular post id = 93 Only.
You may need to use couple of rewrite rules in web site .htaccess file, this file should be placed in root server directory. The mod_rewrite is an Apache module for manipulating (rewriting) URLs. Frequently this means taking the URL requested by a visitor and sending them content at a different URL.
Example:
Original URL:
http://www.example.com/old-page/
Desired destination URL:
http://www.example.com/new-page/
The .htaccess syntax:
RewriteEngine On
RewriteRule ^old-page/?$ $1/new-page$2 [R=301,L]
More info here.
Create an .htaccess and add the below rules in the file
Redirect 301 /this-is-a-test-title/93/ http://www.example.com/?p=93
Redirect 301 /this-is-a-test-title2/93/ http://www.example.com/?p=93
Redirect 301 /this-is-a-test-title3/93/ http://www.example.com/?p=93
there are plugins which help with these kind of redirects.
For e.g.
https://wordpress.org/plugins/quick-pagepost-redirect-plugin/

301 Redirection using .htaccess not working for multiple parameters

I actually want to redirect my old website url to new website url.
I have made 1 redirect working properly
for example
http://www.abc-old.com/test to http://www.abc-new.com/test123
but problem is while i have multiple slug or parameters it won't work for me
like
i want to redirect
http://www.abc-old.com/test1/test2 to http://www.abc-new.com/test1
but it is redirecting like
http://www.abc-old.com/test1/test2 to http://www.abc-new.com/test1/test2
below is my code for same
Redirect 301 /test1/test2 http://www.abc-new.com/test1
Please help me to sort out this
If your .htaccess file looks like / or the httpd.conf contains this it should work.
Options +FollowSymLinks
RewriteEngine on
Redirect 301 /test1/test2 http://www.abc-new.com/test1
Maybe this could be interesting.
http://www.internetmarketingninjas.com/blog/search-engine-optimization/301-redirects/
You can use this rule in old host's root .htaccess:
RedirectMatch 301 "/test1/test2(/.*)?$ http://www.abc-new.com/test1$1
Hie Some how i solved it but still confuse why this thing happned ?
for example I have 3 URL
http://www.old.com/test1/
http://www.old.com/test1/test2
http://www.old.com/test1/test2/test3
I have created 301 redirects in .htaccess like
Redirect 301 /test1/ http://www.new.com/test1
Redirect 301 /test1/test2/ http://www.new.com/test2
Redirect 301 /test1/test2/test3 http://www.new.com/test3
in this case
while i was requesting
http://www.old.com/test1/ is redirected to http://www.new.com/test1
while
http://www.old.com/test1/test2 is redirected to http://www.new.com/test1/test2/
in this case while server execute .htaccess , instead of my second url redirection first was working and additional parameter passing test2 so it new redirect was http://www.new.com/test1/test2
I have write code like this
Redirect 301 /test1/test2/test3 http://www.new.com/test3
Redirect 301 /test1/test2/ http://www.new.com/test2
Redirect 301 /test1/ http://www.new.com/test1
and it started working. i really don't understand why my first code is not working, I have used wordpress CMS , is there any cofiguration required ?

Redirect both directory and files within directory

Need to redirect a directory name to one location while directing file paths within that directory to other locations.
In other words:
Redirect /ORIGdirectoryname/ to /Adirectoryname/
Redirect /ORIGdirectoryname/file.php to /Bdirectoryname/
I am finding that if I write:
redirect 301 /ORIGdirectoryname/ http://yourdomain.com/Adirectoryname/
redirect 301 /ORIGdirectoryname/file.php http://yourdomain.com/Bdirectoryname/
http://yourdomain.com/ORIGdirectoryname/file.php
resolves to
http://yourdomain.com/Adirectoryname/file.php
when I want it to resolve to
http://yourdomain.com/Bdirectoryname/
It may be the order it is in.
If you can imagine the timeline of events, it will go:
/origdirectoryname/(file here) 301 to /Adirectoryname/
Then it won't trigger the second redirect because it's no longer on origdirectoryname. If you redirected the file first, it would go:
/origdirectoryname/file.php 301 to /bdirectoryname/
Then it would not trigger the redirect from origdirectory to adirectory, as you are now on bdirectory.
The Redirect directive links two URL path nodes together. To when you have:
redirect 301 /ORIGdirectoryname/ http://yourdomain.com/Adirectoryname/
You're linking the directory /ORIGdirectoryname/ to /Adirectoryname/, including every file and subdirectory. So going to:
http://yourdomain.com/ORIGdirectoryname/dir1/dir2/dir3/file.php
will redirect you to:
http://yourdomain.com/Adirectoryname/dir1/dir2/dir3/file.php
To prevent this, you can use RedirectMatch instead:
RedirectMatch 301 ^/ORIGdirectoryname/file.php$ http://yourdomain.com/Bdirectoryname/
RedirectMatch 301 ^/ORIGdirectoryname/$ http://yourdomain.com/Adirectoryname/

Categories