HTAccess replace string with hyphen - .htaccess

I had a question about a problem I am having with a rewrite rule, I want to make a rewrite rule that gets everything after the hyphen, but it just gets the last word?
My Link:
http://www.website.com/home/24-nieuws/143-kip-kip-kip-donington.html
I have the following rule:
RewriteRule ^home/24-nieuws/(.*)-(.*).html$ http://www.website.com/$2 [R=301,L]
This rule gives me a weird output, well I find it weird, but It could because a hyphen is used as a regex character?
Output:
http://www.website.com/donington
It skips all of the text and just gets the last word? Does anyone know what I have to do to make it get everything between the first hyphen and the .html?

You can use this rule:
RewriteRule ^home/24-nieuws/[^-]*-(.+)\.html$ http://www.website.com/$1 [R=301,L,NC]
[^-]* is negated character class that match 0 or more non-hyphen characters. It will stop matching before first hyphen.
It is also possible to use a non-greedy quantifier to make it work:
RewriteRule ^home/24-nieuws/.*?-(.+)\.html$ http://www.website.com/$1 [R=301,L,NC]
RegEx Demo

Related

Htaccess - Redirect if URL does not contain at least three numbers

I'm struggling to get this htaccess redirect to work. I want to redirect any URL that does not contain at least three numbers in a row. I started with the following and it worked perfectly for redirecting any URL that DID have three numbers in a row:
RewriteCond %{REQUEST_URI} [0-9]{3,20} [NC]
RewriteRule (.*) "https\:\/\/info\.mywebsite\.com\/" [R=301,L]
However, I tried to modify that with the exclamation mark to make the condition NOT match three numbers in a row:
RewriteCond %{REQUEST_URI} !([0-9]{3,20}) [NC]
RewriteRule (.*) "https\:\/\/info\.mywebsite\.com\/" [R=301,L]
But that doesn't seem to work as expected. Am I missing something with turning this expression into a not match?
Having previously experimented with the opposite 301 (permanent) redirect then the results are most probably cached (by the browser) from the earlier redirect. It is a good idea to test with 302 (temporary) redirects to avoid caching issues.
Note also that the REQUEST_URI server variable contains the URL-path only, so if the digits are contained in the query string part of the URL-path then your condition will fail.
The quantifier {3,20} matches from 3 to 20 characters, if you want "at least three" then use the quantifier {3,} (no upper bound).
You don't need the capturing subpatterns, ie. surrounding parentheses (...) on the regex since you are not using backreferences anywhere. Incidentally, you can't capture subpattern on a negated regex.
You don't need the additional condition (RewriteCond directive) - this can all be done with the RewriteRule directive only.
The NC flag is not required here - you are checking digits only.
For example:
RewriteRule !\d{3,} https://info.mywebsite.com/" [R=302,L]
As noted in comments, the RewriteRule substitution string is a regular string, not a regex, so does not require any special backslash escaping (although colons and slashes don't need escaping anyway in Apache regex).

htaccess Mod Rewrite $_GET to normal URL

I have this Rewrite rule:
RewriteRule ^([a-zA-Z0-9-]+)/([0-9]+)/?$ /home.php?id=$1&slug=$2 [L,QSA]
which currently rewrites things like home.php?id=blog&slug=123 to be /blog/123
how can i change it so the 123 is text and numbers rather than just numbers?
I have this in my htaccess file that is redirecting:
home.php?id=services to /services
i want to also be able to do:
home.php?id=blog&slug=slug-goes-here to be /blog/slug-goes-here
Replace the [0-9]+ part with [\w-]+. This will match the character 0-9, a-z, A-Z, _ (the underscore) and - (the hyphen):
RewriteRule ^([a-zA-Z0-9-]+)/([\w-]+)/?$ /home.php?id=$1&slug=$2 [L,QSA]
Also, you might want to read something about the basics of regular expressions: http://www.regular-expressions.info/

mod_rewrite - check for string

I want to check if a URL contains the sting "-EN.htm", if so apply the rewrite.
That should be done with ^-EN.htm as follows, but the rule is not working:
RewriteCond %{REQUEST_URI} ^/(.*?)/([-_0-9a-zA-Z./=]*)^-EN.htm
RewriteRule ^(.*)$ /indexEN.php?folder=%1&follow=%2 [L]
What am I doing wrong?
Thank you for every help,
Scott
Your regular expression doesn't look right. You can also lose the condition and just move the pattern to the rewrite rule instead. Something along the lines of
RewriteRule ^/?(.*?)/([-_0-9a-zA-Z./=]*)^-EN.htm /indexEN.php?folder=$1&follow=$2 [L]
You need to make the leading slash optional (in htaccess this is stripped off) and instead of using % backreferences, use the $ ones.
Now on to your pattern, it's not valid. The ^ matches the beginning of the string (the URI), so if you have two of them and you're not trying to literally match the ^ character (which you'd need to escape), then the expression will never match anything. Without any examples of URLs that you're having to deal with, I assume you probably just want to ditch the second ^:
RewriteRule ^/?(.*?)/([-_0-9a-zA-Z./=]*)-EN.htm /indexEN.php?folder=$1&follow=$2 [L]

optional second directory in htaccess redirect

Hi
im wanting to write a mod redirect which handles the following:
www.domain.co.uk/brands
rewrites to www.domain.co.uk/index.php?p=brands
www.domain.co.uk/brands/5
rewrites to www.domain.co.uk/index.php?p=brands&go=5
Can this be achieved in one line without a conditional statement?
I have written this, but the second line is ignored:
RewriteRule ^(.*)\.html$ index.php\?p=$1 [L]
RewriteRule ^((.*)/*)(.*)\.html$ index.php?p=$1&go=$2 [L]
Any help would be much appreciated
Well, it seems to me that the first rule would match both versions, and rewrite www.domain.co.uk/brands/5 to www.domain.co.uk/index.php?p=brands/5 -- and then the [L] flag makes the matching stop. It wouldn't match the second rule after the rewrite, anyway.
The second regexp has an asterisk too many (after the slash) and one set of parens too many as well, but if you fix that and move it above the other one, it might help.
Just reverse the order. As a general practice you must have most specific rules first and most generic as last. Try this in your .htaccess:
RewriteRule ^([^/]*)/(.*)\.html$ /index.php?p=$1&go=$2 [L,NC,QSA]
RewriteRule ^(.*)\.html$ /index.php?p=$1 [L,NC,QSA]
This will redirect URI of '/brands/5.html' to /index.php?p=brands&go=5 and a URI of '/brands.html' to /index.php?p=brands
RewriteRule (.*)(/(.*))?$ index.php?p=$1&go=$3 [L]
should do it.
The reason your second rule fails is, you are matching one character (.) followed by a / at the start of the string, so immediately any urls with more than one character before the first slash will fail. You are also insisting that the url ends with html but in your examples they don't. Also for the future, remember . matches any single character so you probably meant to escape the . before html.

htaccess weird trailing slash problem

url: http://localhost/url/of/the/keyword/whatever/
RewriteRule ^url/of/the/keyword/([a-z]+)/?$ ?keyword=$1 [L]
// php
echo $_GET['keyword'];
// outputs **whatever** (OK)
RewriteRule ^url/of/the/keyword/(.*)/?$ ?keyword=$1 [L]
// php
echo $_GET['keyword'];
// outputs **whatever/** (with a trailing slash, which is not expected)
can anyone explain why there's a trailing slash for the second condition?
Also, how can I allow percentage sign in url rewrite?
http://localhost/url/of/the/keyword/%40%23%24/
RewriteRule ^url/of/the/keyword/([0-9a-zA-Z-.%])/?$ ?keyword=$1 [L]
the above rule doesn't work. can anyone correct this so it allows a-Z, 0-9, dot, hyphen, and percentage sign?
Thanks!
You are getting the / for the second RewriteRule because .* is greedy. That is to say it greedily captures the trailing slash because you've marked it as optional /?. It's best to be specific with your patterns (like the first RewriteRule) to avoid such situations.
The pattern you match can accept anything. Just remember it has to be a valid URL. The problem is you forgot the quantifier. So you're only matching one character from the grouping.
Add the +
RewriteRule ^url/of/the/keyword/([0-9a-zA-Z\-.%]+)/?$ ?keyword=$1 [L]

Resources