Too many redirects regex redirect - wordpress

I am on Wordpress and right now using Yoast Seo Pro which has a redirection section including a Regular Expressions redirects section.
How do I redirect say /mycategory1/page/pagenumberhere/ to
/category/mycategory1/page/pagenumberhere/ ?
So I only need to make one redirect that handles all possible page numbers?
I have tried /mycategory1/page/([0-9]) to /category/mycategory1/page/$1
It looks like it redirects to /category/mycategory1/page/pagenumberhere/ but there is an err_too_many_redirects on the /category/mycategory1/page/pagenumberhere/ with this rule added so I have removed it again.
If you can help with code into the .htaccess instead perhaps I could try that.

Your rule already looks promising.
However, as #starkeen pointed out, the regular expression mycategory1/page/([0-9]) matches every request containing mycategory1/page/ with some trailing number. This is true of category/mycategory1/page/ as well, as it also contains "mycategory1/page".
If you want to match requests starting with mycategory1/page, you must anchor the regular expression at the beginning with ^, see Apache mod_rewrite Introduction - Regex vocabulary
RewriteRule ^mycategory1/page/([0-9]) /category/mycategory1/page/$1 [L]

Related

Prevent Wordpress From Rewriting Capital Letters to Lowercase Letters

I am hoping that someone can help me locate where this rewrite is coming from (searched all my nginx files and its not there. I'm guessing it's a WP rewrite in includes?) and help me prevent the rewrite from converting my uppercase path letters to lowercase letters.
Example: If you navigate to this image (below) in a browser please notice that the URL is rewritten. This is not a rewrite I created but it works out because users that are hitting my old blog images which are indexed online will need the rewrite to view them. BUT the uppercase letter 'H' is converted to lowercase 'h' so the user will not be able to view it! If you modify the lowercase to an uppercase in the URL string and hit the page you can now see the image.
Can someone please help me solve this issue? I cannot rename all my images by the way. Also I am using nginx and i hear that you are not aloud to remove case sensitivity from nginx like you can with apache.
Nginx includes a method for converting uppercase to lowercase:
rewrite ^ https://$host$request_uri_low;
make sure you config doesn't have this kind of conversion.

Wordpress htaccess rewriterule without changing URL

In WordPress I'm using the Events Manager plugin, which uses the /events/subpage path in the URL.
I need to tweak things slightly so that /courses/subpage rewrites to /events/subpage i.e. the content of /events/subpage is shown when going to /courses/subpage. The URL should stay as /courses.
I thought this rule would do it:
RewriteRule ^courses/?(.*)$ /events/$1 [L]
But the URL changes from /courses to /events so it looks like my rule isn't quite right.
EDIT
Strangely I can put anything in place of /courses and it resolves to /events anyway. Confused!
Think I’ve cracked it.
I uninstalled the rewrite plugin and replaced it with Rewrite Rules Inspector. Then I added the following to functions.php
add_rewrite_rule(‘^course/([^/]*)/?’, ‘index.php?event=$matches[1]‘, ‘top’); // single event
In Rewrite Rules Inspector is said the rule was missing. I flushed the rules using the plugin and everything appears to be working now. It looks like the original rewrite plugin wasn’t flushing the rules properly.

How do I create a rule that will fire only on my site's home page?

I using Google Tag Manager and I am trying to setup a rule that will fire ONLY on my sites homepage.
The issue is that I am not certain how to handle all of the URL permutations of the homepage. How can I create a rule that will handle:
"http://" "https://" "http://www." "https://www."
Also, we use Sitecore and support multiple languages, so the homepage url can also display as:
"http://www.mysite.com/en"
I am not sure how to handle the culture identifier that is inserted into the URL path after a visitor has used the navigation on the site.
Is it possible to use the OOTB Google Tag Manager rules to handle this scenario, or will I have to implement a Tag Manager Data Layer?
The following rule would check if it's the homepage:
{{url}} matches RegEx ^https?://(www\.)?mysite\.com/?(index\.html)?$
{{url}} gives the whole address whereas {{url domain}} just gives the domain and {{url path}} just the path (including the initial forward slash).
This matches http and https, with or without www and with or without index.html at the end. It also matches mysite.com/ and mysite.com (without the forward slash at the end). If you want to check for URL permutations at the end of the homepage, you could do something like:
^https?://(www\.)?mysite\.com/?(en|es|fr)?$ etc.
Also, forward slashes do NOT have to be escaped. In fact, escaping forward slashes broke the firing rule in GTM for me...
See http://en.wikipedia.org/wiki/Regular_expression
edit: and if you want to ignore the querystring (which is a good thing to do because most ads add query keys such as utm_source etc. to the url), you can have something like this:
^https?://(www\.)?mysite\.com/?(index\.html)?/?(\?.*)?$
(note the (\?.*)? at the end)
Ok... so after researching the Google Tag Manager Forum, this can be accomplished by making separate url "ends with" rules for your site url and then your site url with a trailing forward slash such as:
rule 1 : url ends with http://mysite.com
rule 2 : url ends with http://mysite.com/
I think it was the trailing slash that was confusing the matter as I was setting up the rules.
This is a super robust way to know it's your home page with either protocol and allow for a backslash all with one line of Regex...
^https?://www.mydomain\.com\/?$
Not sure sure why the initial double backslashes don't have to be escaped, but it works. Would have expected ^https?:\/\/www.mydomain\.com\/?$ Maybe someone else knows that :)

IIS 7.0 Url Rewrite2 Rule and Regex Issues

I am using IIS.net's Url Rewrite program on my server to make rules for URL Rewrite. I have pages on my Asp.NET 4.0 Web Site such as Page.aspx, and News.aspx.
When I create a rule for Page.aspx (http://www.foo.com/Page.aspx?PageTitle=Foo to http://www.foo.com/Foo) It works great.
When I create a rule for News.aspx (http://www.foo.com/News.aspx?NewsTitle=Foo to http://www.foo.com/Foo) It works great too.
But when I create rules for both News.aspx and Page.aspx, it fails. Program looks rule order. If News's rule is at top, News.aspx Url rewritten, and Page.aspx Url rewrite fails.
If Page's rule is at top, Page.aspx Url rewritten, and News.aspx Url rewrite fails.
I guess thhis problem occurs because of regex matching. Let me show you:
Rule for News.aspx is at top. So it works fine. But when I click Page.aspx, the RewriteUserFriendlyURL1's Pattern catch my URL, and tries to redirect it News.aspx (Shown in Action Url).
I hope my description is clear.
My question is: How can I avoid this problem? For example I can make a new page called Redirect.aspx, and can redirect all pages to here. I can make 1 rule for Redirect.aspx.
But I think there should be another way to solve this issue with configuring these rules.
Thank you.
The problem is that the regular expressions for RewriteUserFriendlyURL1 and RewriteUserFriendlyURL2 are exactly the same so in that case the order in which they are listed decides which rule gets applied first.
I understand what you are trying to accomplish but it can not work like that. It's impossible for the URL rewrite module to know if /Foo is a news article or a page. Your best option is probably to put the news under /news/<title> and keep /<title> for your regular pages. In that case you can use /news/ to make a difference between news and pages.
So you should keep RedirectUserFriendlyURL2 and RewriteUserFriendlyURL2 the same but change the action URL for RedirectUserFriendlyURL1 to /news/{C:1} and change the pattern for the RewriteUserFriendlyURL1 rule to ^news/([^/]+)/?$ (note: no slash between ^ and news).

Evaluating a external rewrite rule before internal wordpress rewrite rule

I am been plucking out my hairs since last few days trying to solve this problem:
I want to convert my urls from format
http://example.com/prodsearch/category/tag1-tag2-tag3-tag4
to
http://example.com/prodsearch/index.php?tag=tag1+tag2+tag3+tag4&cat=category
to start with I added following rules to my .htaccess
RewriteRule ^prodsearch/(.*)/(.*)-(.*)$ prodsearch/$1/$2+$3 [L]
RewriteCond %{REQUEST_URI} !^prodsearch/(.*?)/(.*)\-(.*)
RewriteRule ^prodsearch/(.*)/(.*)$ index.php?tag=$2&cat=$1 [R,QSA,L]
Here rule1 recursively replaces all '-' between tags by '+'
and rule2 does the actual rewrite once there are no more '-' left between tags (checked by RewriteCond)
These rules actually work, but the problem is they redirect (with url change) to new url pattern, I don't want an explicit redirection. On removing 'R' flag from last rule, the whole thing stops working and I start getting 404 page in my wordpress install.
Can some one explain why this is happening and how to do this without explicit redirection.
Also I tried including these rules into wordpress, hoping when called by wordpress rules may work without redirection. I used following add_rewrite_rule() calls:
add_rewrite_rule('prodsearch/(.*)/(.*)-(.*)$','price-list/$1/$2+$3','top');
add_rewrite_rule('prodsearch/(.*)/(.*)$','index.php?tag=$matches[2]&cat=$matches[1]','top');
Now wordpress detects my first rule as external rule and flushes it to .htaccess file, this screws my execution order, now first rule 2 gets evaluated first(being part of internal wordpress rewrite rule set) and rule 1 gets executed later, hence again 404 page.
Is it possible to tell wordpress not to consider my first rule as a external rule and not to flush it to .htaccess.
Or does anyone has any idea to make this kind of rewrite work? Thanks a lot.

Resources