I have the following htaccess rule to rewrite my URL
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^app/(.*)/(.*)/(.*)$ app/index.php?app_released=$1&app_name=$2&app_version=$3 [L,NC]
</IfModule>
Which rewrites to app/3-25-2018/name/version that's fine, but when user tries to put more slashes and some random values after the slash for example app/3-25-2018/name/version/something/else/here it should redirect them to my custom 404 page but I'm receiving undefined app_released error by PHP because, it's not able to read the GET variable properly. How can I fix this?
The problem lies in the regular expression you use and particularly (.*), which matches all characters including /. Instead, supplant (.*) with ([^\/]*) which matches all characters besides /.
Correct Regex:
^app\/([^\/]*)\/([^\/]*)\/([^\/]*)$
See the above regular expression in action in the following examples:
matching app/3-25-2018/name/version - here.
not matching app/3-25-2018/name/version/something/else/here - here.
Related
I have index.php with only one line in it
<?php echo $_GET['a']; ?>
And also I have a .htaccess with only two lines in it
RewriteEngine on
RewriteRule ^([a-z]+)$ ?a=$1
Why when I enter /ab.cd I get a 404 error, but if I entering /abcd. it doesn't give me a 404 error and PHP echos abcd (without the dot on the end of the url).
Can anybody tell me why my rewrite rule allows abcd. through instead of giving me a 404 error?
The regular expression you've used, ([a-z]+), matches only the lowercase letters "a" through "z". It will never match a series of letters followed by a period followed by a series of letters, like your attempt with ab.cd.
If you want to match periods as well, you need ([a-z.]+).
EDIT:
I've just tested your Rewriterule (standalone) in my dev environment and it works. It doesn't allow /abcd. through on mine - It gives me a 404 error, so there must be something somewhere in your environment that's affecting your rules.
-initial post-
Looks like the period(.) in your /ab.cd isn't defined in the Rewriterule - just lowercase a-z chars.
Use:
RewriteEngine on
RewriteRule ^([a-z.]+)$ ?a=$1
^
I just rebuilt the situation on my server and I'm getting the desired 404 error. I only added the [L] flag to the RewriteRule.
.htaccess
RewriteEngine on
RewriteRule ^([a-z]+)$ test.php?a=$1 [L]
test.php
<?php echo $_GET['a']; ?>
Are there maybe any other rewrite rules following that might interfere with your request? Even in other files that get included? In that case, the L flag might help you.
I have a URL: search/?word=asdf and want to redirect to: search/word/asdf/ and running internally: ?cmd=search&word=asdf
This so you can get the PHP $ _GET ['cmd'] and $ _GET ['word'].
How to do it in htaccess?
EDIT:
My .htaccess now is:
RewriteRule search(.*) %{HTTP_REFERER}cmd/search$1
RewriteRule cmd/search/?key-word=(.*) %{HTTP_REFERER}cmd/search/key-word/$1
But this not working. The new URL ever is:
localhost/bruc/sandbox/electrolux/trunk/cmd/search/?key-word=asdf
But it should be: localhost/bruc/sandbox/electrolux/trunk/cmd/search/key-word/asdf
So, I redirect this correct URL to: localhost/bruc/sandbox/electrolux/trunk/?cmd=search&key-word=asdf
But not working fine! Try, my approach here: http://htaccess.madewithlove.be/
Try RewriteRule ^([^/]*)/word/([^/]*)$ /?cmd=$1&word=$2 [L]. I believe that will accomplish your goal.
Try this :
RewriteEngine on
RewriteRule ^search/word/(.*)$ /?cmd=search&word=$1 [L]
Check this.
RewriteEngine on
RewriteRule ^([^/]+)/([^/]+)/([^/]+) /?cmd=$1&word=$2 [L]
There are three parts to this:
RewriteRule specifies that this is a rule for rewriting (as opposed to a condition or some other directive). The command is to rewrite part 2 into part 3.
This part is a regex, and the rule will be run only if the URL matches this regex. In this case, it says - look for the beginning of the string, then a bunch of non-slash characters, then a slash, then another bunch of non-slash characters. then again bunch of non-slash characters, then a slash, then another bunch of non-slash characters. The parentheses mean the parts within the parentheses will be stored for future reference.
Finally, this part says to rewrite the given URL in this format. $1 and $2 refer to the parts that were captured and stored.
My .htaccess file looks like this:
RewriteEngine On
RewriteRule ^articles/(\d+)*$ ./articles.php?id=$1
So, if the URL foo.com/articles/123 is requested, control is transferred to articles.php?id=123.
However, if the requested URL is:
foo.com/articles/123/
or
foo.com/articles/123/whatever
I get a "404 Not Found" response.
I would like to call articles.php?id=123 in all these cases. So, if the URL starts with foo.com/articles/[digits]... no matter what other characters follow the digits, I would like to execute articles.php?id=[digits]. (The rest of the URL is discarded.)
How do I have to change the regular expression in order to achieve this?
Just don't look for the end:
RewriteRule ^articles/(\d+) ./articles.php?id=$1
You do need to allow the trailing / with:
RewriteRule ^articles/(\d+)/?$
The \d+ will only match decimals. And the $ would disallow matches beyond the end.
If you also need trailing identifiers, then you need to allow them too. Then it might be best to make the match unspecific:
RewriteRule ^articles/(.+)$
Here .+ matches virtually anything.
But if you want to keep the numeric id separate then combine those two options:
RewriteRule ^articles/(\d+)(/.*)?$ ./articles.php?id=$1
I want (for example)
discuss/1/title/where-are-you
to output
discussPage.php?id=1&title=where-are-you
I have the following commands in a .htaccess file which is in the same directory as discussPage.php:
# Enable Rewriting
RewriteEngine on
# Rewrite user URLs
# Input: discuss/ID/title/TITLE
# Output: discussPage.php?tid=NAME&title=TITLE
RewriteRule ^discuss/(\w+)/(\w+)/(\w+)/?$ discussPage.php?id=$1&title=$3
Right now I am getting a 404 error when I try to visit a page like discuss/1/title/where-are-you even though discussPage.php?id=1&title=where-are-you works fine.
P.S. mod_rewrite is enabled (i have used it for other purposes).
Your problem is that \w does not match the "-" in the last bit.
You can simply allow dashes in there using a broader character class like [\w-]+. I would be careful with solutions like [^/] because they allow anything except a slash, which is quite permissive. White-lists are always safer and avoid surprises.
# Enable Rewriting
RewriteEngine on
# Rewrite user URLs
# Input: discuss/ID/title/TITLE
# Output: discussPage.php?tid=NAME&title=TITLE
RewriteRule ^discuss/(\w+)/(\w+)/([\w-]+)/?$ discussPage.php?id=$1&title=$3
Note that the - has to be last in the character class, otherwise you should escape it, because if it's in between two other characters it behaves as a range, like in [a-z].
To debug, you can always echo $_SERVER['QUERY_STRING'];
But I believe the correct code would be something like this.
RewriteEngine on
RewriteBase /
RewriteRule ^discuss/([0-9]+)/title/([a-z0-9\-]+)?$ discussPage.php?id=$1&title=$2 [NC,QSA]
You might want to fix your RewriteBase value, though.
Also - as seen in the last condition, you can only have alphanumeric characters and hyphens in the where-are-you part. (but something tells me you won't be needing other characters!)
Works for me:
RewriteEngine On
RewriteBase /
RewriteRule ^discuss/([^/]+)/([^/]+)/([^/]+)/?$ discussPage.php?id=$1&title=$3 [R]
I'm trying to pass an URL as a parameter in mod-rewrite. I guess there is a problem in my Regex. This my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule **^go/((http:\/\/)+[A-Za-z0-9\-]+[\.A-Za-z])/?$** feedmini.php?url=$1 [L]
</IfModule>
the URL I want to pass looks like http://www.aaaa.com/aaa/?q=v but when ever I try to reach it on go/http://www.aaaa.com/aaa/?q=v I get an 404 error page. I've also tried with **^go/([A-Za-z0-9\-\/:]+[\.A-Za-z]+)/?$** but then the URL i pass gets like this: http:/www.aaaa.com/aaa/ (observe the singel '/' after 'http:');
Any Ideas?
Thanks in advance
/Ale
Well your first problem (in your first code block) is that your Regex pattern will not match a URL since it will only match a string that begins with http:// then contains nothing but alphanum or dashes, which ends with a single fullstop or letter. Perhaps this is simply a typo and there should be a quantifier in there, but even so it would fail to match a very large percentage or URLs.
This may seem a little strange, but try this...
RewriteRule ^go/http:/(.*)/?$ feedmini.php?url=http://$1 [R=302,L]