RegEx: Rewrite URL with exception - regex

I am trying to change the permalink structure of my Wordpress site and setting up proper redirects. Here is what I am trying to achieve:
Old permalink structure:
/category/post-name
New permalink structure:
/postname
Redirecting would be fairly simply done by:
Source:
/category/(.*)
Destination:
/$1
However, the base URL without the post-name must not be rewritten. So if someone access /category directly (without specifying a post-name) it should not redirect, because /category is a valid page. It should only redirect if something is following /category.
Example:
https://somesite.com/category -> Don't redirect
https://somesite.com/category/post-123 -> redirect URL to https://somesite.com/post-123
Any ideas how I can do this? Note: I am using the Yoast Plugin and its RegEx redirect option.
I am not a coder, so sorry for my gibberish. I hope I am making some sense.
Thank you! :)

Use .+ to match at least one character:
/category/(.+)

Related

How can I remove the email in the end of the url

I'm using WooCommerce. I don't know which plugin caused the issue with the url
https://example.com/color/black/page/23/support#domain.com
https://example.com/shop/march-for-science-on-washington-dc-t-shirt/support#domain.com
Is there anyway to track down which one made these url on Google Webmaster?
I'm looking for the solution to rewrite these urls either.
For example, it will remove the email and return to the correct url:
https://example.com/color/black/page/23/
https://example.com/shop/march-for-science-on-washington-dc-t-shirt/
I used nginx to rewrite it
rewrite ^/(.*)/support#domain.com /$1 permanent;

301 redirects - replace a section of the URL

I'm re-working a Woocommerce store, and I need to set up redirects that will send the old URLs, which include category names, to the new URLs, which won't.
EDIT: Both the old and new URLs will contain product/ and the old URL may or may not have a trailing slash.
An example would be:
Old URL: myshop.co.uk/product/category-name/sub-category-name/product-name
New URL: myshop.co.uk/product/product-name
Can anyone tell me what the reg exp would be to achieve this?
Search for (?<=product/)(?:[^/]+/)+(?=[^/]+/?$) and replace with an empty string.

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)