Conditional rewrite in the regex, not with RewriteCond - apache

I wrote a regex for a rewrite rule that includes a negative lookahead to replace the rewriteCond line, because WordPress only accepts two values: pattern -> substitution.
It should find findme.html _here, regardless of where it's requested to be:
mydomain.com/_here/findme.html
e.g.
(Sorry, I can't modify the swf which will request findme.html in the wrong places)
So, given findme.html could be requested to be in, e.g.:
mydomain.com/findme.html
mydomain.com/directory/findme.html
mydomain.com/directory/findme.html?someparam=3
The rewrite should make them all
mydomain.com/_here/findme.html
So, I made a rewrite rule that Wordpress will accept me as follow
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^.*(?!_here)/*findme\.html$ /_here/findme.html [R=301,L]
So it only matches URLs which doesn't contain "_here" in it, to prevent extra rewriting or a loop.
The problem is IT DOES loop.
What did I miss?

It looks to me like you want to move the .* that is before (?!_here) to after it because (?!_here) is a negative lookahead, so it check that the text _here does not come after it. What your regular expression is actually checking is whether your url starts with some character sequence that is not followed by _here, and _here is a character sequence not followed by _here. Then your rule becomes
RewriteRule ^(?!_here).*/*findme\.html$ /_here/findme.html [R=301,L]
Also, it looks like your pattern will exclude paths with subdirectories such as
mydomain.com/directory/subdirectory/findme.html
If you also want to include those, the pattern should be
RewriteRule ^(?!_here)(.*/)*findme\.html$ /_here/findme.html [R=301,L]

Related

How to redirect URL using .htaccess with dynamic parameter?

I want to redirect
https://example.com/product-info/A100001
to
https://example.com/product-info/index/index/id/A100001
using htaccess redirect rule
A100001 will be dynamic like
A100001
A100002
A100003
A100004
....
I am trying this
RewriteEngine on
RewriteCond %{QUERY_STRING} product-info/A100001
RewriteRule ^$ /routing/index/index/id/? [L,R=301]
Source
Also tried other example but not working in my scnario
Anyone who expert in htacees rules can help me in this.
RewriteCond %{QUERY_STRING} product-info/A100001
RewriteRule ^$ /routing/index/index/id/? [L,R=301]
Your example URL contains a URL-path only, it does not contain a query string. The rule you've posted would redirect /?product-info/A100001 to /routing/index/index/id/.
Try something like the following instead:
RewriteRule ^(product-info)/(A\d{6})$ /$1/index/index/id/$2 [R=302,L]
The above would redirect a request of the form /product-info/A123456 to /product-info/index/index/id/A123456.
The $1 backreference simply contains product-info, captured from the RewriteRule pattern (saves repitition) and $2 contains the dynamic part (an A followed by 6 digits).
This is a 302 (temporary) redirect. Always test first with a 302 to avoid potential caching issues.
The order of directives in your .htaccess file is important. This rule will likely need to go near the top of the file, before any existing rewrites.
UPDATE:
redirection is working with your code, Can you please let me know the parameter pattern, I need the number from A452218 to A572217
Regex does not handle numeric ranges, only character ranges. If you specifically only want to match numbers in the stated range then you would need to do something (more complex) like this:
RewriteRule ^(product-info)/A(45221[89]|4522[2-9]\d|452[3-9]\d{2}|45[3-9]\d{3}|4[6-9]\d{4}|5[0-6]\d{4}|57[01]\d{3}|572[01]\d{2}|57220\d|57221[0-7])$ /$1/index/index/id/A$2 [R=302,L]
NB: The $2 backreference now only contains the dynamic number, less the A prefix, which is now explicitly included in the substitution string.

Redirect a URL with two query string parameters to a new domain with a changed path using Apache's rewrite rules

I'm trying to do the following thing:
Transform URL: https://example.com/human/?system=jitsi&action=SYSTEM
to: https://new.example/?action=SYSTEM&system=jitsi
RewriteEngine On
RewriteCond %{REQUEST_METHOD} GET
RewriteCond "%{QUERY_STRING}" action=(.+)
RewriteRule / https://new.example/?action=SYSTEM&system=%1?
Unfortunately I can't get the expected result.
Your rules have several problems:
You aren't matching the human/ in the URL path that you are trying to remove.
Neither your condition nor your rule has regex markers for "starts with" (^) or "ends with" ($) which means that they are matching substrings rather than full strings.
Your rule starts with a slash which means that it can't be used in .htaccess, only in Apache's conf files. I recommend starting rules with an optional slash /? so that they can be used in either context.
Your query string condition is only matching one of the two parameters that you say need to be present to redirect.
(.+) in your query string condition can match too much. It would include other parameters separated by an ampersand (&). This should be changed to ([^&]+) meaning "at least one character not including ampersands"
You have an extra stray ? at the end of your target URL that would be interpreted as part of the value of the system parameter.
You don't explicitly specify flags for redirect ([R]) or last ([L]) which are best practice to include.
RewriteEngine On
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{QUERY_STRING} ^system=([^&]+)&action=([^&]+)$
RewriteRule ^/?human/$ https://new.example/?action=%2&system=%1 [R,L]

htaccess rewrite condition that uses regex

I'm a noob when it comes to regex. What I'm trying to accomplish is:
https://www.example.com/shop/product-floating-front-rotor-kit/
should redirect to
https://www.example.com/shop/product-matching-front-rotor/
product should be the name of the product, I have to do this for multiple products.
Edit: This is what I have so far, am I even close?
RewriteEngine On
RewriteRule ^/shop/([a-z]+)-floating-front-rotor-kit/ ^/shop/$1-matching-front-rotor/
RewriteRule ^/shop/([a-z]+)-floating-front-rotor-kit/ ^/shop/$1-matching-front-rotor/
This is close, except that...
In .htaccess the URL-path matched by the RewriteRule pattern (first argument) does not start with a slash.
The substitution string has an erroneous ^ prefix. This should be an "ordinary" string, not a regex.
[a-z] does not match hyphens/dashes, which you state could occur in a product name.
You have not included an end-of-string anchor ($) on the end of the RewriteRule pattern, so any trailing string will be successful and discarded. (Is that the intention?)
This is an internal "rewrite", not a "redirect" as stated. You need to include the R flag. (An internal rewrite is unlikely to work here, since the target URL requires further rewriting.)
Try the following instead. This should go at the top of the .htaccess file, immediately after the RewriteEngine directive.
RewriteRule ^shop/([a-z-]+)-floating-front-rotor-kit/$ /shop/$1-matching-front-rotor/ [R=302,L]
This is a 302 (temporary) redirect.

Mod Rewrite Map file not returning correct results

Im using the following code in the mod rewrite. I have a search parameter variable that I set before and use in the rewrite condition against a map file. The Condition requires the map file to return a result but then I use the same mapfile in the rewrite rule and it returns and empty string.
RewriteCond ${root301:%{ENV:SEARCHURL2}|NOT-FOUND} !(^$|NOT-FOUND) [NC]
RewriteRule ^(.*)$ /Page1?search2=${root301:%{ENV:SEARCHURL2}} [QSA,NE,R=301,L]
so for example http://mysite/root1 is suppose to go to http://mysite/page1?search2=results but it ends up just going to http://mysite/page1?search2= with no search parameters. if the map condition wasn't satisfied the rewrite wouldn't happen at all? Im using compiled map files and I see the dictionary being compiled correctly.
I realized the issue I had was that I actually had 2 similiar redirects one was looking for a url without the trailing forward slash but when I duplicated the first one I forgot to add the trailing forward slash to the redirect rule like below. thus the search conditions weren't identical so of course it didn't work
RewriteCond ${root301:%{ENV:SEARCHURL2}/|NOT-FOUND} !(^$|NOT-FOUND) [NC]
RewriteRule ^(.*)$ /Page1?search2=${root301:%{ENV:SEARCHURL2}} [QSA,NE,R=301,L]

Apache URL Rewrite to domain.com/custom_url_name

Using Apache on a Red Hat server, I'm trying to rewrite the URL of a member's store on our website from:
domain.com/store.php?url=12345
to:
domain.com/12345
Using these rules, I can get it to work if I always remember to add an ending slash:
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^store/url/(.*)$ store.php?url=$1
RewriteRule ^(.*)/$ store.php?url=$1
domain.com/12345/ works,
but domain.com/12345 does not work.
Removing the slash in the last line of Rewrite code breaks a lot of stuff. Is there a way to get this to work both with or without that ending slash?
What if you made the slash optional? Furthermore, you probably to to specify something more specific than (.*), because domain.com/a/b/c/d/e will match. Instead, you can use a negated character class to specify everything other than a slash.
RewriteRule ^([^/]*)/?$ store.php?url=$1
Alternately, if you only want to capture numbers, you can use the \d shorthand class (which matches any digit) along with a + which specifies that at least one digit must be present:
RewriteRule ^(\d+)/?$ store.php?url=$1
Your attempt using ^(.*)$ fails because that would match any URL path. Use a more specific pattern than .*, maybe \d+ to allow only one or more digits:
RewriteRule ^(\d+)$ store.php?url=$1