htaccess Mod Rewrite $_GET to normal URL - .htaccess

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/

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).

301 Redirecting all articles from a old folder to a new folder

I need your help regarding redirect rules.
I have an old URL structure:
www.example.com/en/news/news/single/article/SPECIFIC-ARTICLE/
(with SPECIFIC-ARTICLE being a placeholder for a whole bunch of articles - with a trailing slash!).
After the relaunch the name and structure of the folders will change to:
www.example.com/en/news/SPECIFIC-ARTICLE
So all articles (with trailing) in the old structure should be redirected to the corresponding articles in the new structure. And shouldn't have a trailing slash anymore.
How should the mod_rewrite rule in .htaccess look like?
And how would this rule change if there wasn't a trailing slash but a .html at the end of the old article URLs?
Using mod_rewrite in .htaccess this would be something like (near the top of the file):
RewriteEngine On
RewriteRule ^(en/news)/news/single/article/([\w-]+)/$ /$1/$2 [R=302,L]
$1 is a backreference to the en/news match in the RewriteRule pattern (this is simply to avoid repetition). $2 is a backreference to the SPECIFIC-ARTICLE (which is assumed to consist of just the letters A-Z, a-z, 0-9, _ and -).
If there is .html instead of the trailing slash, then change the RewriteRule pattern to:
RewriteRule ^(en/news)/news/single/article/([\w-]+)\.html$ /$1/$2 [R=302,L]
Note the dot is backslash escaped to match a literal dot and not any character.
Note that this is a temporary (302) redirect, change this to a 301 (permanent) redirect only when you are sure it is working OK.

allow Special Characters in URL

I want to allow special charecters in URL
my htaccess code is
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^blog/?$ SocialNetwork/blog.php [L]
RewriteRule ^blog/(\d+)/?$ SocialNetwork/blog.php?id=$1 [L]
RewriteRule ^blog.php/(\d+)/?$ SocialNetwork/blog.php?id=$1 [L]
RewriteRule ^blog.php/(\d+)/([A-Za-z0-9-_[]]+)/?$ SocialNetwork/blog.php?id=$1&title=$2 [L]
</IfModule>
it works for
blog/1
blog/1/hii
blog/1yooo_title
but does not work for
blog/1/[hii-this is [] title
In your last rewrite rule, try escaping the square brackets. Also if you need to match spaces you need to include \s in there:
^blog.php/(\d+)/([A-Za-z0-9-_\[\]\s]+)/?$
Or perhaps consider the simpler which accepts anything in the title:
^blog.php/(\d+)/(.+)/?$
Another point is that you should not have spaces in your URLs. They should be escaped to "+" or %20. So depending on this your regex would change, except the last one I proposed should work.
I'm pretty sure that you can't use spaces in a URL. Ever.
only alphanumerics, the special characters "$-_.+!*'(),", and reserved characters used for their reserved purposes may be used unencoded within a URL.
http://www.ietf.org/rfc/rfc1738.txt

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]

htaccess rewrite ONLY if first part of path is numeric

Is there an htaccess rule that will only rewrite if the first part of a path is numeric, so that http://www.example.com/123/whatever hits the rewrite rule, but http://www.example.com/user/whatever does not?
Here is a rewrite rule for my little site I am building
RewriteEngine on
RewriteRule ([a-zA-Z])/ index.php?k=$1
RewriteRule ([0-9]+)/ index.php?id=$1
So you can see that the regex rule [0-9]+ will match any numbers successively. The [a-zA-Z] will match letters.
You can match numbers in your pattern. For example:
RewriteRule ^([0-9]+)/(.*) /foo/$2?bar=$1
Will rewrite http://www.example.com/123/whatever to http://www.example.com/foo/whatever?bar=123 but leave /user/whatever alone.

Resources