Htaccess string exact string match - apache

I have a minor problem, I have two URLs that are similar that need separate redirects, is there a way to ensure a full match in the htaccess file?
E.G
/duck
and
/duckcat
we've tried putting /duckcat above /duck however, we are receiving matches on users via /duck that matches to /duckcat
Edit:
RewriteRule ^/duckcat http://www.store.com/store/xx/stores/duckcat [R=301,NC,L]
RewriteRule ^/duck http://www.store.com/store/xx/stores/duck [R=301,NC,L]
in the above example if the user is coming in from duck they match on duckcat.

Related

Redirection with alphanumeric digit numbers

I need to redirect everything from the first "/" in my domain. For example
I need to redirect this: https://audiobookscloud.com/B07D5HYCR2
For that: https://example.com/B07D5HYCR2
But I need to ensure that this only happens when there are 10 digits after the first slash, that is: ".com/"
My solution was this:
RewriteRule ([^/]+)/\d{10}?$ https://www.example.com/$1 [L,R=301]
But it doesn't work as expected.
How can I resolve this?
Your matching pattern does not match what you describe. What you implemented is that: Any string that contains any non empty string that does not contain a slash, followed by a slash and nothing else or exactly 10 digits. A RewriteRule is only applied to the path component of a requested URL. So just to the /B07D5HYCR2 in your example and not to something like audiobookscloud.com/B07D5HYCR2, as you apparently expect.
Change the matching pattern slightly to come closer to what you describe:
RewriteRule ([^/]{10})?$ https://www.example.com/$1 [L,R=301]
Though that will redirect a few more URLs than you want, according to your description. Which is why I would recommend some further changes:
RewriteRule ^/?[0-9A-Z]{10}$ https://www.example.com%{REQUEST_URI} [L,R=301]
That variant precisely matches all request paths that contain exactly 10 characters that are either a digit or a capital letter, with nothing before or behind that.
I really recommend to take a look into the documentation of the tools you are trying to use. Here that would be the apache rewriting module. It is, as typical for OpenSource software, of excellent quality and comes with great examples: https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html

htaccess url redirect with get parameters ID and reduce value

I want to do an url redirect to a new domain by retrieving the ID parameter but only taking the first 4 characters. Anyone know how to do this?
For example, an original url:
http://www.original.example/see/news/actualite.php?newsId=be9e836&newsTitle="blablabla"
To :
https://www.new.example/actualites/be9e
I have tested :
RewriteCond %{QUERY_STRING} ^newsId=(.*)$ [NC]
RewriteRule ^$ https://www.new.example/actualites/%1? [NC,L,R]
RewriteCond %{QUERY_STRING} ^newsId=(.*)$ [NC]
RewriteRule ^$ https://www.new.example/actualites/%1? [NC,L,R]
There are a couple of problems with this:
The regex ^$ in the RewriteRule pattern only matches the document root. The URL in your example is /see/news/actualite.php - so this rule will never match (and the conditions are never processed).
The regex ^newsId=(.*)$ is capturing everything after newsId=, including any additional URL parameters. You only need the first 4 characters of this particular URL param.
As an aside, your existing condition is dependent on newsId being the first URL parameter. Maybe this is always the case, maybe not. But it is relatively trivial to check for this URL parameter, regardless of order.
Also, do you need a case-insensitive match? Or is it always newsId as stated in your example. Only use the NC flag if this is necessary, not as a default.
Try the following instead:
RewriteCond %{QUERY_STRING} (?:^|&)newsId=([^&]{4})
RewriteRule ^see/news/actualite\.php$ https://www.new.example/actualites/%1 [QSD,R,L]
The %1 backreference now contains just the first 4 characters of the newsId URL parameter value (ie. non & characters), as denoted by the regex ([^&]{4}).
The QSD flag (Apache 2.4) discards the original query string from teh redirect response. No need to append the substitution string with ? (an empty query string), as would have been required in earlier versions of Apache.
UPDATE:
I have an anchor link (#) which is added at the end of the link, is there a possibility of deleting it to make a clean link? Example, currently I have: https://www.new.example/news/4565/#title Ideally : https://www.new.example/news/4565
The "problem" here is that the browser manages the "fragment identifier" (fragid) (ie. the "anchor link (#)") and preserves this through the redirect. In other words, the browser re-appends the fragid to the redirect response from the server. The fragid is never sent to the server, so we cannot detect this server side prior to issuing the HTTP redirect.
The only thing we can do is to append an empty fragid (ie. a trailing #) in the hope that the browser discards the original fragment. Unfortunately, you will likely end up with a trailing # on your redirected URLs (browser dependent).
For example (simplified):
:
RewriteRule .... https://example.com/# [R=301,NE,L]
Note that you will need the NE flag here to prevent Apache from URL-encoding the # in the redirect response.
Like I say above, browsers might handle this differently.
Further reading:
URL Fragment and 302 redirects
redirect is keeping hash
How to clear fragment identifier on 302 redirect?

How does one add something to the end of an url with mod_rewrite

I need to add ?lang=English to an url /self-service/more here if a visitor comes from a specific domain. (not comes from, thats impossible but i meant was first at site A and then clicked a link and went to my site)
How can i do that? I tried reading the manual but its actually a bit over my head
Try using the mix of rewrite condition and rewrite rule like this, placed on top of .htaccess
RewriteCond %{HTTP_REFERER} ^(http|https)://best-graphic-design\.co\.uk [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1?lang=English [R=301,L,QSA]
A simple explanation
First check for the referer to your site, NC flag stands for nocase/ignore case
If condition matches, redirect to desired location, in your case, same host while appending the new parameter, flags used here are R - Redirect, L - Last, QSA - Query String Append.
Hope it finally helps.

URL Rewrite query

I want to understand what is meaning of these Rewrite rules
RewriteRule ^([^/]+)/([^/]+).html /title.php?file=$1&sub1=$2 [NC]
And
RewriteRule ^admin/reg/([^/]+) /admin.php?file=$1 [NC]
Kindly give me example, how it would be redirected to title.php
If you went to http://yourdomain/yomomma/house.html it would redirect to /title.php?file=yomamma&sub1=house
The second one http://yourdomain/admin/reg/candyfloss would redirect to /admin.php?file=candyfloss
NC makes it case insensitive.
Check out this page for more information on Regex (which is what's used in the first part of the RewriteRule) Regex Quickstart. Regex basically allows you to search strings. The example you posted picks up any characters except backslashes (which are used to break your parameters anyways).

URL rewriting with mod_rewrite to provide RESTful URLs

The web server is Apache. I want to rewrite URL so a user won't know the actual directory. For example:
The original URL:
www.mydomainname.com/en/piecework/piecework.php?piecework_id=11
Expected URL:
piecework.mydomainname.com/en/11
I added the following statements in .htaccess:
RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.mydomainname\.com$ [NC]
RewriteRule ^(w+)/(\d+)$ /$1/%1/%1.php?%1_id=$2 [L]
Of course I replaced mydomainname with my domain name.
.htaccess is placed in the site root, but when I access piecework.mydomainname.com/en/11, I got "Object not found".(Of course I replaced mydomainname with my domain name.)
I added the following statements in .htaccess:
RewriteRule ^/(.*)/en/piecework/(.*)piecework_id=([0-9]+)(.*) piecework.mydomainname.com/en/$3
Of course I replaced mydomainname with my domain name.
.htaccess is placed in the site root, but when I access piecework.mydomainname.com/en/11, I got "Object not found".(Of course I replaced mydomainname with my domain name.)
What's wrong?
Try using RewriteLog in your vhost or server-conf in order to check the rewriting process. Right now you just seem to guess what mod_rewrite does.
By using RewriteLogLevel you can modify the extend of the logging. For starters I'd recommend level 5.
PS: Don't forget to reload/restart the server after modifying the config.
Here's a quick overview of what's happening:
RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.mydomainname\.com$ [NC]
First, the question mark is supposed to be at the end.
$1 would (should) match anything that is not 'www' 0 or 1 times.
$2 matches anything that is not a character 1 or more times, which theoretically would match a blank space there but likely would never match anything.
Then it requires '.mydomainname.com' after those two groupings.
Your first two conditions are looking for two separate groupings.
I'm not sure exactly how you're trying to set up your structure, but here is how I would write it based on your original and expected URL's:
RewriteCond %{HTTP_HOST} !^www\.mydomainname\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(\w+)\.mydomainname\.com$ [NC]
RewriteRule ^(\w+)/(\d+)$ /$1/%1/%1.php?%1_id=$2 [L]
Basically, your first condition is to make sure it's not the URL beginning with 'www' (it's easier to just make it a separate rule). The second condition makes it check any word that could possibly be in front of your domain name. Then your rewrite rule will redirect it appropriately.
Get rid of the last .htaccess line there in your question...
Someone please correct me if I typed something wrong. I don't remember if you have to have the '\' in front of '\w' and '\d' but I included them anyways.
You are doing it backwards. The idea is that you will give people the friendly address, and the re-write rule will point requests to this friendly, non-existent page to the real page without them seeing it. So right now you have it only handling what to do when they go to the ugly URL, but you are putting in the friendly URL. since no rule exists for when people put the friendly URL directly, apache looks for it and says "Object not Found"
So add a line:
RewriteRule piecework.mydomainname.com/en/(*.) ^/$3/en/piecework/$3?piecework_id=([0-9]+)(.*)
Sorry, that's quite right, but the basic idea is, if they put in the URL you like, Apache is ready to redirect to the real page without the browser seeing it.
Update
I'm way to sleepy to do regex correctly, so I had just tried my best to move your example around, sorry. I would try something more simple first just to get the basic concept down first. Try this:
RewriteRule www.mydomainname.com/en/piecework/piecework\.php\?piecework_id\=11 piecework.mydomainname.com/en/11
At the very least, it will be easier to see what isn't working if you get errors.