htaccess rewrite ONLY if first part of path is numeric - .htaccess

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.

Related

.htaccess How to solve redirection with ignore

I want to solve this issue in my .htaccess
I want to redirect all urls that match eg. /2017/02/some-slug to /some-slug
BUT I want to ignore URLs like this /2017/02/page/3
I've managed how to do the first part:
RewriteRule ^/([0-9]{4})/([0-9]{2})/([a-z0-9-\/]+)$ /$3 [R=301,L]
I have problems with the second part which is ignore. Any ideas?
You can use negative lookahead in your regex pattern:
RewriteRule ^/?\d{4}/\d{2}/(?!page/\d+/?$)(.+)$ /$1 [R=301,L,NC,NE]
Here (?!page/3/?$) is negative lookahead that asserts failure when year/month/ is followed by page/3.

Difficulty Rewriting URL using .htaccess

I've seen several answers but none of them have worked for me.
How do I rewrite the below using my .htaccess? mod_rewrite is on in my file.
http://example.com/local/vendor/index.php?handle=Company
to be:
http://example.com/local/vendor/Company
How do I rewrite the below...
There's possibly a more fundamental problem here. You don't rewrite it that way round in .htaccess. You rewrite from the "friend" URL back to the actual URL/filesystem path. So, it's the other way round, you rewrite from:
http://example.com/local/vendor/Company
to the actual URL your application is expecting:
http://example.com/local/vendor/index.php?handle=Company
For Example:
RewriteEngine On
RewriteRule ^(local/vendor)/(Company)$ /$1/index.php?handle=$2 [L]
$1 and $2 are backreferences to the captured groups in the RewriteRule pattern.
The above matches just the specific URL you stated, ie. /local/vendor/Company. I suspect that "Company" is intended to be a placeholder? In which case you need to make this more general. For example:
RewriteRule ^(local/vendor)/(\w+)$ /$1/index.php?handle=$2 [L]
\w is a shorthand character class for word characters. This excludes the dot (ie. .) so avoids a rewrite loop when rewriting to index.php.
UPDATE: The above assumes your .htaccess file is located in the document root, ie. example.com/.htaccess. However, if the .htaccess file is located in the /local subdirectory (ie. example.com/local/.htaccess) - as you appear to suggest in comments - then you will need to adjust the above directives to remove local/ from the RewriteRule pattern and remove the slash prefix from the substitution. For example:
RewriteRule ^(vendor)/(\w+)$ $1/index.php?handle=$2 [L]

htaccess redirect only if it has value after forward slash

I would like to apply the following htaccess rule only if there is a value after the forward slash of /events/
If I visit /events/ (Don't do anything. DO NOT apply the htaccess rule)
But if I visit /events/this-can be-anything/ (Apply the htaccess rule below)
RewriteRule ^events/(.*) /lp/events/index.php [L]
This is obviously not working.
You need to change your regex pattern (.*) to (.+) to match at least one character after the uri.
RewriteRule ^events/(.+)$ /lp/events/index.php [L]

htaccess url masking mod rewrite

I have done some URL masking and it all works very nicely. I have one issue that I am trying to resolve now:
RewriteRule ^(.*)/(.*)/clubs/(.*)/$ teams.php?competition=$1&season=$2&teamid=$3
with the above I can access the same page 2 ways, www.domain.com/premier-league/2010-2011/arsenal/ and www.domain.com?competition=premier-league&season=2010-2011&teamid=arsenal
is there a way in my rewrite rule I can redirect the URL (301 ideally) is someone does it through the untidy way "www.domain.com?competition=premier-league&season=2010-2011&teamid=arsenal"?
Thanks in advance
If you add another rewrite rule after this one that matches your 'inverse' pattern, and mark both as the [L]ast rule, that might work. I first rewrite the url to include the query string, and [C]hain that one to the next rule. After that we [R]edirect the browser.
RewriteRule ^(.)/(.)/clubs/(.*)/$ teams.php?competition=$1&season=$2&teamid=$3 [L]
RewriteRule ^(.+) $1%{QUERY_STRING} [C]
RewriteRule ^teams.php?competition=([a-zA-Z0-9]+)&season=([a-zA-Z0-9]+)&teamid=([a-zA-Z0-9]+)$ $1/$2/clubs/$3/ [R=301,L]
Note I haven't tested this or the regex of the second rule. Might need to adjust the character ranges a bit. Also I haven't tested the query string rewrite in the second rule.
Edit: See here for some common use cases of mod_rewrite: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

htaccess rewrite losing $_GET on second rewrite?

Here are my two rewrites:
RewriteRule folder/(.*)/$ /folder/subfolder/index.php?s=$1
RewriteRule folder/(.*)/review/$ /folder/subfolder/review.php?s=$1
The first rewrite works perfectly, for example:
http://www.site.com/folder/hello/
But the second:
http://www.site.com/folder/hello/review/
It doesn't pass the "hello" as the $_GET over.
Why is the second rewrite losing its parameter?
Because it's reading (.*) to include "...site.com/folder/hello/review/" which then doesn't match any rewrite rule.
Change the rewrite rule to:
RewriteRule folder/([0-9a-zA-Z-]+)/review/$ /folder/subfolder/review.php?s=$1
That will limit it to alpha-numeric and "-" characters only.

Resources