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

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

Related

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

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

Remove Category from the base URL in wordpress

My post URL structure is http://example.com/slug and the category URL is http://example.com/category/category-name.
Currently, my custom structure is /%postname%/ and the category base is blank.
I would like to change the category URL to http://example.com/category-name. How do I go about this?
I tried with htaccess but got 404 error:
RewriteRule ^category/(.+)$ http://www.yourwebsite.com/$1 [R=301,L]
You're receiving a 404 because you're sending users from http://example.com/category/category-name to http://example.com/category-name, which doesn't actually exist as a page, yet.
The RewriteRule is a redirect, rather than framing the URL.
From the sounds of things, you actually have a category base set within Wordpress (hence the "category/" before the category-name), which can be removed by going to "Settings" -> "Permalinks" -> "Category base" in the Wordpress panel.
In the event you're also using YoastSEO, you may wish to reference this documentation from Yoast.

htaccess permanent redirect - url with slug while address bar shows slug url

From:
https://example.com/item.php?id=123
Desired (redirect to):
https://example.com/item123/slug
RewriteEngine On
RewriteRule ^item([0-9]+)/([^/.]+)/?$ /item.php?id=$1 [QSA]
above code works but only some of the time and I am stuck. The slug is NOT significant (disregarded and only for SEO) as I use the item number to figure out where to redirect to (also needs a permanent redirect 301).
Thank you in advance.

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.

Remove part of URL

i have a url like this http://example.com/blog/photos/photos/gallery/image/1.
And i need to remove the second photos folder. How do i remove the part using mod_rewrite and .htaccess?
For your interest /blog is my document root.
Thanks a lot for any suggestions, Steve
EDIT
You should know that the URLs being generated by Wordpress 3.0 und NextgenGallery.
http://example.com/blog is my document root. That means i have installed Wordpress into the folder blog.
The first slug after blog is the page i have my gallery associated with.
The second slug is the name of the album and could be renamed to everything you want. It is just a placeholder for my galleries. gallery is the name of the gallery.
Assuming you want to do a redirect:
RewriteRule ^/blog/photos/photos/(.*)$ /blog/photos/$1 [R]