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
Related
I have a website, let's say fruit.com, and currently I have a bunch of redirects set up that work just fine, so for example fruit.com/apples/mcintosh will redirect to fruit.com/apples.php?id=mcintosh.
I also used to have some redirects set up to allow me to use a short URL, so fru.it/mcintosh would redirect to fruit.com/apples.php?id=mcintosh.
So far so good. A few years ago, though, my short domain lapsed and I didn't renew. Recently I've purchased it again and I'm interested in getting the same setup back.
Now, though, the redirects from the short domain to the main domain aren't working, although I've used exactly the same code, so I'm at a bit of a loss for what's going wrong.
RewriteCond %{HTTP_HOST} ^www\.fru\.it$
RewriteRule ^([0-9]+)$ "http\:\/\/www\.fruit\.com\/apples.php?id=$1" [R=301,L]
although I've used exactly the same code
But the code you've posted won't redirect the stated example URL fru.it/mcintosh, since the code matches digits only, not letters.
Try the following instead:
RewriteCond %{HTTP_HOST} ^www\.fru\.it
RewriteRule ^(\w+)$ http://www.fruit.com/apples.php?id=$1 [R=301,L]
The \w shorthand character class matches upper and lowercase letters, numbers and underscore.
You don't need all the backslash-escapes in the substitution string.
Also bear in mind that the order of these directives can be important. This rule would likely need to go near the top of the .htaccess file to avoid conflicts.
Test first with a 302 (temporary) redirect to avoid potential caching issues. Clear your browser cache before testing.
Aside:
fruit.com/apples/mcintosh will redirect to fruit.com/apples.php?id=mcintosh
It would seem to make more sense that this would be a (internal) "rewrite", not a (external) "redirect"? The shortcode would then redirect to fruit.com/apples/mcintosh, not fruit.com/apples.php?id=mcintosh?
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.
I am having some difficulty trying to format my redirect correctly.
I have the following:
RedirectMatch 301 http://mooseburger.com/onlinestore/index.cgi?code=3&cat=7
https://www.mooseburgeronline.com/categories/Clown-Costumes-/Coats%2C-Jackets-%26-Vests/
I know it has something to do with the % or & signs. How do you escape them?
I believe the pattern of the RedirectMatch should be an absolute URL (like "/onlinestore/something") or an URL relative to the htaccess' location (like "onlinestore/something"). Also, the RedirectMatch pattern can't match against query strings, unfortunately. You'll need to use mod_rewrite for that, as Rekire suggested. The syntax would be something along these lines:
RewriteEngine on
RewriteCond %{QUERY_STRING} \bcode=(3)\b
RewriteCond %{QUERY_STRING} \bcat=(7)\b
RewriteRule ^onlinestore/index.cgi https://www.mooseburgeronline.com/categories/Clown-Costumes-/Coats%2C-Jackets-%26-Vests/? [NC]
This also strips off the original query string. I haven't tested it, but this should get you started.
Some links:
The docs are, hm, somewhat hard to read, but it's full of information. Make sure to check the control flow diagram, it helps a lot.
Query string cheat sheet
Regex tutorial for the intricacies of patterns.
And of course Google will give you hundreds of decent tutorials.
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).
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.