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.
Related
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.
I really don't understand why my .htaccess file works wrong with ^(.*)$ ...
This is my .htaccess:
RewriteEngine On
RewriteRule ^(.*)$ index.php?alias=$1 [L]
I simply want to put all the text after my host address in the GET parameter named "alias".
The result of this, is that in my PHP script "index.php", $_GET['alias'] will have the value "index.php" instead what is after my host address.
If I change my .htaccess like this:
RewriteEngine On
RewriteRule ^([A-Za-z\/]+)$ index.php?alias=$1 [L]
I'll finally have $_GET['alias'] with the right value.
For example if I request http://myaddress.com/the-value-of-alias, in the first case I'll get $_GET['alias'] = 'index.php'. In the second case instead, I'll get $_GET['alias'] = 'the-value-of-alias'.
Then could someone explain me what are the differences between ^([A-Za-z\/]+)$ and ^(.*)$ please?
Thank you very much in advance. :)
When you put .* it matches any url so you end up in a recursive redirection whereas when you have specific regular expression elements it will work as it matches your first url only. It does not match the destination url as it has a 'equal-to' symbol in it
The ^(.*)$ expression means anything but a dot.
As for the expression ^([A-Za-z\/]+)$ it will get the Characters from A to Z as well as a to z
I am trying to convert urls like this http://example.com/page.php?user=x&post=x into
http://example.com/blogs/user/post
This is the code I have so far, not sure if I'm missing something or have done something wrong as it haven't been successful so far.
RewriteEngine On
RewriteRule ^blogs/([a-z0-9\-]+)/([0-9]{1,11})/$ page.php?user=$1&post=$2 [L]
EDIT: After making some amendments, it returns now an error 404 error saying page.php is not found. I know for certain the file is there.
2ND EDIT: Resolved 404 issue.
The post parameter is optional as well.
user can will have a mix of A-z0-9 (no character limit)
post can only be 0-9 with and upto 11 characters in length
According to http://htaccess.madewithlove.be/, this should work:
RewriteEngine On
RewriteRule ^blogs/([a-z0-9-]+)/([0-9]+)$ /page.php?user=$1&post=$2 [L]
This transforms http://example.com/blogs/abc_123/67890 to http://example.com/page.php?user=abc_123&post=67890.
The most notable change is that you apparently can't have a leading / in the first component of the rule, but I also removed the {11} constraint on the post ID since it's unlikely that would have the intended effect.
Try
RewriteRule ^blogs/?([a-z0-9]*)?/?$ page.php?user=$1&post=$2 [NC,L]
The ? indicates that the matching is optional. Therefore for /? the slash is optional.
And for the second section after the ^blogs/?
([a-z0-9]*)?
If you remove the ? and * from the above to become like this :
RewriteRule ^blogs/?([a-z0-9])/?$ page.php?user=$1&post=$2 [NC,L]
The user will no longer become optional and must be fully matched.
Example http://blogs/John;
If you put + in place of * like below:
RewriteRule ^blogs/?([a-z0-9]+)/?$ page.php?user=$1&post=$2 [NC,L]
The user need to be matched with at least 1 character. For example : http://blogs/J
And if you put * asterisk like below:
RewriteRule ^blogs/?([a-z0-9]*)/?$ page.php?user=$1&post=$2 [NC,L]
It can match to zero length. For example : http://blogs/ . Almost like optional.
If the match failed you will get the 404 Not found error
This code provides the desired results...
RewriteRule ^blogs/([a-z0-9]+)?/?$ page.php?user=$1 [NC,L]
RewriteRule ^blogs/([a-z0-9]+)?/?([0-9]+)?/?$ page.php?user=$1&post=$2 [NC,L]
I also amended the post ID constraint as Fraxtil pointed out.
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.
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]