new to mod_rewrite. I simply want to make something like this:
# Turn on the rewriting engine
RewriteEngine On
# use item php page to show the item
RewriteRule ^parts/./[.]$ parts/item.php?id=$1 [NC]
so when a person types in example.com/parts/anything/anything, the URL will go to
example.com/parts/item.php?id=the2ndanything
From my understanding, the period means any character. Also, I tried to add a + after the period(which should mean as much as anything), but that still didn't work.
by the way, I am on a temporary URL, since I have not changed my previous web host yet, and therefore my actual full URL has a ~ in it. I am hoping this is not an issue for mod_rewriting and therefore being the issue that stops it from working. the .htaccess file is in the root folder with the web sites index.html.
The . does mean any character, but you must follow it by + (one or more) or * (zero or more) and you must capture it in () to be used as $1. Use [^/]+ to match all characters up to but not including / to match but not capture the first directory after parts/.
RewriteEngine On
# (.*) captures the second "anything" in $1
RewriteRule ^parts/[^/]+/(.*)$ parts/item.php?id=$1 [L]
Related
I have the following URL:
https://sub.example.com/economy/billing/payment-check/434/khipu/1000/CLP/1673526088/
I want the last 5 directories to be converted to GET values, to reflect the following URL:
https://sub.example.com/economy/billing/payment-check/?id=434&gateway=khipu&amt=1000&curr=CLP&ts_ttPageLoad=1673526088
I have the following .htaccess lines:
RewriteEngine on
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/ index.php?id=$1&gateway=$2&amt=$3&curr=$4&ts_ttPageLoad=$5 [QSA,L]
Over at https://htaccess.madewithlove.com/ I get the following rewrite result:
https://sub.example.com/index.php?id=economy/billing/payment-check/434&gateway=khipu&amt=1000&curr=CLP&ts_ttPageLoad=1673526088
What am I doing wrong? Thank you!
EDIT: The .htaccess is located in https://sub.example.com/economy/billing/payment-check/
The regex quantifier * is greedy by default so consumes as much as possible and then backtracks as required. This is why $1 contains economy/billing/payment-check/434 (ie. as much as possible that satisfies the regex). Although, this is assuming the .htaccess file is in the document root (which is what the "MWL tester" assumes).
If you are wanting to match a single path segment only then exclude the slash from the capturing subpattern, eg. ([^/]+).
Also, from the nature of your rule, I'm assuming this .htaccess file is in the /economy/billing/payment-check/ subdirectory (not the root)? The "MWL tester" tool assumes the .htaccess file is located in the document root (and this cannot be changed). In fact, this would "work" on your server if the .htaccess file is located in the subdirectory, however, it is not optimal, as it could still potentially match too much given a variation in the URL structure.
If the .htaccess file is located at /economy/billing/payment-check/.htaccess then use the following instead:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?id=$1&gateway=$2&amt=$3&curr=$4&ts_ttPageLoad=$5 [QSA,L]
The regex [^/]+ matches something except a slash.
If, however, the rule is in the root .htaccess file (which the MWL tester is expecting) then the rule would need to look like this:
RewriteRule ^(economy/billing/payment-check)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ $1/index.php?id=$2&gateway=$3&amt=$4&curr=$5&ts_ttPageLoad=$6 [QSA,L]
Note that the first capturing subpattern is now the URL-path, that is repeated in the substitution string (2nd argument). This simply avoids having to manually repeat the URL-path in the 2nd argument.
It looks like your URL is expecting specific data types in the path segments. For instance, the 1st, 3rd and 5th "parameters" look to be digits only. If this is the case then you should be specific and match digits only. Likewise, the 4th "param" is uppercase and the 2nd is lowercase letters. So, being specific, this could be rewritten like this (with the .htaccess file in the subdirectory):
RewriteRule ^(\d+)/([a-z]+)/(\d+)/([A-Z]+)/(\d+)/$ index.php?id=$1&gateway=$2&amt=$3&curr=$4&ts_ttPageLoad=$5 [QSA,L]
It then only matches the format of URLs you are expecting.
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 am developing a Symfony2 PHP application. In my Wamp server, the application is stored in www/mySite/ and my index.php is www/mySite/web/app_dev.php. Because/ of that, I have URL like 127.0.0.1/mySite/web/app_dev.php
I wanted to change the path so I acces my index file just by typing 127.0.0.1. After some research, I figured out that writting this .htacces in the www folder works :
RewriteEngine on
Rewritecond %{REQUEST_URI} !^/mySite
Rewriterule ^(.*)$ /mySite/web/app_dev.php
The only problem is that I don't understand why. Does somebody explain it to me ?
I don't really understand the two last line, and regex like ^(.*)$
Thanks
This is a simple regex indeed:
^(.*)$
Let's break it up:
^ - begging of a string
( and ) - capture group, used to match part of a string
. - any character
.* - any charactery any number of times
$ - end of a string
So, putting it all together, it means: "match any number of any characters". Later this matched part (part in parentheses) is replaced by /mySite/web/app_dev.php.
To explain regexes a little bit more we could imagine different regexes:
^lorem.*$ - string starting with word "lorem" followed by any number of any characters
^$ - an empty string
^...$ - a string containing three characters.
Now, putting it all together - Apache's rewrite rules are usually built of two directives: RewriteCond and RewriteRule. The latter directive will affect only those requests which match the condition given in the RewriteCond. You can think of them as a "if-then" pair:
# the "if" part - if request URI does not match ^/mySite
Rewritecond %{REQUEST_URI} !^/mySite
# the "then" part - then rewrite it to "/mySite/web/app_dev.php"
Rewriterule ^(.*)$ /mySite/web/app_dev.php
Rewritecond %{REQUEST_URI} !^/mySite
Check and make sure the requested uri does not("!") start with("^") "/mySite"
Rewriterule ^(.*)$ /mySite/web/app_dev.php
Then if that is true, take things starting with("^") any character(".") any amount of times("*") and send it to "/mySite/web/app_dev.php"
So a URI of /controller/site-action will be sent to that file while /mySite/css/style.css would not be.
Many places to check that will give a breakdown and explanation: http://regex101.com/
Regular expressions work character after character. In your `.htaccess it checks if the current URI matches the regex. In this image, follow the line character after character and it returns true:
^ and $ stand for the beginning and end of a string.
. allows any character and * tells to "repeat the last rule as often as possible".
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]