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]
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 could not find a satisfying answer nowhere, and i know very basics about Rewrite Rules, but just can't find a way to achieve that. I would like to clip a certain portion of my address:
/entry.php?id=howdy-world
so, I would like it to look like this:
/entry/howdy-world/
I know how to point to a default file, or Rewrite get values to become numbers after the trailing slash, but the portion to be trimmed is kind of in the middle. How to deal with that?
Below is the basic syntax of how to forward from one url to another. The below has a few sections
([^/] +): This matches any character multiple times up to a slash. ex: entry/howdy-world/
$1: This would take what was after entry/ and append it to the required url. ex: entry.php?id=howdy-world
More information on rewriting can be found on the Apache website
RewriteEngine On
RewriteRule ^entry/([^/]+)$ entry.php?id=$1 [L]
# Variations of above rule depending on server setup
# RewriteRule ^entry/(.+)$ /entry.php?id=$1 [L]
# RewriteRule ^entry/(.+)$ http://www.example.com/entry.php?id=$1 [L]
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.
I have situation that I need to direct all request matching http://mywebsite.com/portfolio/[anyname] to http://mywebsite.com/portfolio.php?username=[anyname]. Can anybody help me write .htaccess file for that.
Thanks
You can likely get away with a simple rule like:
RewriteEngine On
RewriteRule ^portfolio/(\w+)$ portfolio.php?username=$1 [L]
If the usernames can contain anything else but letters, use .+ instead of \w+
The other options are documented under http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html