How make 301 redirects for strange urls found in GSC (possibly hacked site) - htaccess - regex

I'm calling on you because I have a problem that I can't solve! I did a lot of research on Stack Overflow without really finding a solution.
As said in the title, I found in the Google Search Console 1800 pages with strange urls. (These urls do not exist in my www or if I crawl the site). The solution to this problem would be to make 301 redirects from the second child of the urls concerned with regex
Strange URLs 1
https://www.example.com/x1/x2/x3/x4/x2/x4/x5.php ( x1 is normally a php page : https://www.example.com/x1.php and x2 is a folder containing php pages )
Hundreds of pages have a path that starts with https://www.example.com/x1/x2/... I think we should redirect in 301 x1/x2 to home
I tried with
RewriteRule ^x1 url/ [R=301,L]
I redirect well to the home but I no longer have access to url/x1.php
RewriteRule ^x1/x2 url/x1.php [R=301,L]** Internal Server Error
RewriteRule ^x1/$ url/ [R=301, L]** Internal Server Error
RewriteRule ^x1$ url [R=301, L]** Internal Server Error
Strange URLs 2
https://www.example.com/x1.php/x2/x3/x4.php ( x1.php is a category page and x2 is a folder containing php pages )
Hundreds of pages have a path that starts with https://www.example.com/x1.php/x2/... I think we should redirect in 301 x1.php/x2 to the home
Strange URL 3
https://www.example.com/x1/x2.php/x3.php ( x1 is a folder containing php pages )
Hundreds of pages have a path that starts with https://www.example.com/x1/x2.php/... I think you should redirect 301 x1/x2.php to home
Thank you for your help and sorry for my bad English

Related

htaccess replace one parameter with other in url

I have installed WordPress in in subdirectory.
My url is domain/blog/post-title
now I want domain/pages/post-title
only want change blog with pages, how i can do it in htaccess so all my old url's can 301 redirect on new url?
Thanks

mod_rewrite is appending rule to unrelated links ( side-effect )

RewriteRule ^products/([^/]*)$ /products.cgi?id=$1 [L]
Will rewrite /products.cgi?id=$1 as /products/$1 as expected. But it is also appending /products/ to all links in the page. Suppose i have a link in the products page that is:
Contact
Then the products page will link to:
/products/contact-us.cgi
That is not something i desire. I want to keep it /contact-us.cgi

301 redirect domain and all its pages

We have 2 sites on the same niche and basically with the same content, we are going to just close one of them and 301 everything to the one we will be keeping, content is basically the same the only difference is basically the url structure of some pages:
In the one to be 301:
www.domaintobeclosed.com/browse-{keyword}-url string.html
In the one to be redirected to:
www.domaintokeep.com/browse-url string.html
So here the only difference is that in the domain we are keeping the url structure doesnt have the keyword.
The other difference is that in single page url structure:
In the one to be 301:
www.domaintobeclosed.com/title.html
in the one to be redirected to:
www.domaintokeep.com/keyword/title.html
So the difference is that the domain to keep has a keyword before the actual title
Any help on this will be greatly appreciated.
Thanks in advance.
So something like:
RedirectMatch 301 ^/(browse)-keyword-(.*)$ http://www.domaintokeep.com/$1-$2
RedirectMatch 301 ^/([^/])\.html$ http://www.domaintokeep.com/keyword/$1.html

301 redirect from old to new URL without querystring and?

I'm trying to redirect from my old URL structure to a new one where in my case some parameters are added.
The old structure looked like www.url.com/detail/111.html which is rewritten from www.url.com?action=detail&id=111
Now the structure changed for SEO reasons, new the URL for the site mentioned above is something like www.url.com/detail/111/cat/sub.html.
Now i want to redirect from
www.url.com/detail/111.html to
www.url.com/detail/111/cat/xyz.html
or from
www.url.com/detail/112.hmtl to
www.url.com/detail/112/cat/abc.html
The last part in the new URLs is variable!
If im doing it with:
Redirect 301 /detail/112.html http://www.url.com/detail/112.cat/abc.html
the querystring ist added to the new url. The browser shows: http://www.url.com/detail/112.cat/abc.html?action=detail&id=112
When I'm adding a ? to the new URL the ? is also shown in the browser.
QSD doesn't work because Apache 2.2 is running.
Has anyone an idea what to do to solve the problem?
You can put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^detail/112\.html$ http://www.url.com/detail/112.cat/abc.htm? [L,NC,R]
Take note of ? at the end of target URL that is used to strip off any existing query string.

How to redirect a http request with apache / django

I've made a simple site in Django. The urls I use are http::/www.example.com/nl/ and http://www.example.com/fr/.
My Django urls.py has the following line:
(r'^(?Pnl|fr)/', 'example.views.index'),
In example.views.index I check the language parameter. If it's 'nl' I show a template. If it's 'fr', I show a different template.
This worked great. Now the customer made two different urls:
http://www.dutch.com/ and http://www.french.com/
And finally I'll ask the question:
Is there a way for me to use the new urls without changing my django code? I assume I can tell apache to present the http://www.example.com/nl/ page when the user goes to http://www.dutch.com/. But how do I do this? And will django still be able to get the 'language' parameter from the url?
Thanks in advance for any answers.
If you can use .htaccess files on http://www.dutch.com that you can use apache's redirect directive like so
redirectMatch 301 ^(.*)$ http://www.example.com/nl/
This will redirect all requests sent to dutch.com to example.com/nl
You could also use
redirect 301 /index.html http://www.example.com/nl/
This will redirect only "index.html" on dutch.com to example.com/nl/ (note that the first parameter is a path and can't be an URL - no http://www)