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
Related
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.
I need a solution for my redirect problem. In .htaccess file I have this code:
RewriteEngine On
RewriteRule ^category/([a-zA-Z0-9]+) category.php?page=$1 [NC,L,QSA]
And this is working for a query like
category.php?page=pinturas and redirect to category/pinturas.
But, if the string have - like this:
category.php?page=aquoso-madeira it redirects to category/aquoso
Any help?
category.php?page=aquoso-madeira It redirect to category/aquoso
Because your RewriteRule pattern does not include the hyphen (-), so it matches everything up to, but excluding, the first hyphen.
Include the hyphen in the character class:
RewriteRule ^category/([a-zA-Z0-9-]+) category.php?page=$1 [NC,L,QSA]
Note that the hyphen (-) must go at the start or end of the character class, since this is a special character and has alternative meaning when used elsewhere.
And this working for query like category.php?page=pinturas and redirect to category/pinturas
Note that this rewrite does the complete opposite of what you are describing.
This rule takes a request like category/pinturas and internally rewrites it to category.php?page=pinturas. (There is no external redirect here - which might be in another part of your code?)
I'm trying to URL rewrite using .htaccess
from
example.com/daily.php to example.com/daily (and example.com/daily/)
with the following code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule daily/$ daily.php
however:
example.com/daily/ = ok
example.com/daily = not ok
RewriteRule daily/$ daily.php
In the above RewriteRule directive, daily/$ is a regular expression (regex) that matches against the URL-path in the request. This regex contains a trailing slash (/), so this will clearly not match a URL that does not end in a slash.
If you want to match both /daily/ and /daily (although I would not recommend this - see note below) then you need to make the trailing slash optional in the regex. You make this character optional by following it with ? (question mark). For example:
RewriteRule ^daily/?$ daily.php [L]
I've also included a start-of-string anchor ^, so it only matches /daily and not /<anything>daily. You will probably want the L (last) flag, if you plan on adding any more directives.
Aside: If you allow both /daily/ and /daily, which are technically two different URLs then you potentially have "duplicate content". You should choose one or the other as the canonical URL. And optionally route the non-canonical version to the other.
I want rewriting "cat/" => x.php
"cat/sample-title" => y.php?t=$1 with htaccess.
I writed rewrite rules;
RewriteRule ^cat/ x.php [L]
RewriteRule ^cat/(.*)$ y.php?s=$1 [L]
but always cat/ rewriting to y.php?s=$1
You're seeing two issues. The first is that both the first and second rules' patterns are matched by ^cat/, so the second one supplants the first after it is rewritten. You will need to anchor it with $, and also may want to permit an optional trailing / with /?.
Then, to differentiate it from the first, the second one must match one or more characters following the cat/, so instead of (.*), you should use (.+).
I would also change their order (though it is not strictly necessary in this case)
# Ensure one or more characters with .+
RewriteRule ^cat/(.+)$ y.php?s=$1 [L]
# Anchored with optional trailing /
RewriteRule ^cat/?$ x.php [L]
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]