I want to do a mod_rewrite URL transformation. I want to direct
http://localhost/events?user=XXX&start=YYY&total=ZZZ
to
http://localhost/?operation=events&user=XXX&start=YYY&total=ZZZ
What I am using currently in my .htaccess is:
RewriteRule
^events\?userid=([0-9]+)&start=([0-9]+)&total=([0-9]+)$
?operation=events&user=$1&start=$2&total=$3
[L]
but it does not seem to be working.
I have also tried a simpler version:
RewriteRule ^events\?([a-zA-Z0-9&=]+)$ ?operation=events&$1 [L]
But it is also not working.
¿How can I "transfer" either the query params one by one OR else the whole query string to the end of the directed path?
How can I "transfer" either the query params one by one OR else the whole query string to the end of the directed path?
This is done using QSA flag. QSA (Query String Append) flag preserves existing query parameters while adding a new one.
However remember that you cannot match query string using RewriteRule directive. You can only match Request URI using RewriteRule.
You can use this rule:
RewriteEngine On
RewriteRule ^/?events/?$ ?operation=userevents [L,NC,QSA]
This will route http://localhost/events URI to http://localhost/?operation=userevents by appending all the original query parameters to rewritten URI. For example: http://localhost/userevents?user=XXX&start=YYY&total=ZZZ will be rewritten to http://localhost/?operation=events&user=XXX&start=YYY&total=ZZZ
Related
How force add get parameter to url, that already have a get param? for example:
To url like this, which must have that ?a param, value doesnt matter:
example.com/?a=value
Make like this, value2 hardcoded:
example.com/?a=value&b=value2
I tried, yes its for index.php all:
RewriteRule index.php/?a$ /index.php/?b=value2 [L,R,QSA]
RewriteRule index.php/?a$ /index.php/?b=value2 [L,R,QSA]
The RewriteRule pattern (first argument) does not match against the query string. If you specifically need to check that the query string starts with the a parameter then you need a seperate condition that checks against the QUERY_STRING server variable.
Assuming your URL actually contains index.php (your example doesn't?) and the URL-path doesn't end in a slash (ie. contain path-info) then you could do:
# Check that query string contains the "a" URL parameter anywhere
# but does not contain the "b" URL parameter already (avoid redirect loop)
RewriteCond %{QUERY_STRING} \ba=
RewriteCond %{QUERY_STRING} !\bb=
RewriteRule ^index\.php$ /index.php?b=value2 [QSA,R,L]
The \b matches a word boundary, so a can only match at the start of the query string or at the start of a URL parameter (after &).
The QSA flag appends (ie. merges) the original query string from the request. So, it will redirect from /index.php?a=value to /index.php?b=value2&a=value - note that the original query string is appended.
If you specifically wish to keep the URL parameters in the original order and append the new value to the end then you could change the RewriteRule to the following:
RewriteRule ^index\.php$ /index.php?%{QUERY_STRING}&b=value2 [R,L]
Now the result would be /index.php?a=value&b=value2.
I am trying to Rewrite my mode using this info[copy from a website], but it's not working .. what's wrong with it and plz give a correct answer..
The Apache rewrite engine is mainly used to turn dynamic url’s such as www.yoursite.com/product.php?id=123 into static and user friendly url’s such as www.yoursite.com/product/123
RewriteEngine on
RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]
Another example, rewrite from:
www.yoursite.com/script.php?product=123 to www.yoursite.com/cat/product/123/
RewriteRule cat/(.*)/(.*)/$ /script.php?$1=$2
You seem to have misunderstood what the rewrite module (and your rules specifically) actually does.
Quite simply when you browse to:
localhost/abc/product.php?id=23
The RewriteRule isn't invoked and shouldn't be doing anything. There's nothing wrong here, you're just browsing to the wrong URL
URL Transformation
http://www.yoursite.com/product/123 <- URL that user goes to
http://www.yoursite.com/product.php?id=123 <- Rewritten URL that the server sees
RewriteRule(s) explanined
A rewrite rule is broken down into three parts (not including the RewriteRule part...)
The regex that it matches against the url
The url that it transforms into
Additional options
Given your rule:
RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]
The regex is: ^product/([^/.]+)/?$
The new url : product.php?id=$1
The options : [L]
This means that the user browses to the nice url http://www.yoursite.com/product/123 (and all of your links point to the nice URLs) and then the server matches against the regex and transforms it to the new URL that only the server sees.
Effectively, this means that you have two URLs pointing to the same page... The nice URL and the not-nice URL both of which will take you to the same place. The difference is that the not-nice / standard URL is hidden from the general public and others pursuing your site.
Regex
The reason why http://mysite.com/product/image.jpg is not being redirected is because of the regex that you use in your RewriteRule.
Explanation
^product/([^/.]+)/?$
^ => Start of string
product/ => Matches the literal string product/
([^/.]+) => A capture group matching one or more characters up until the next / or .
/?$ => Matches an optional / followed by the end of the string
Given the URL:
http://mysite.com/product/image.jpg
Your regex matches product/image but then it encounters . which stops the matching... As that isn't the end of string $ the rule is invalidated and thus the transform never happens.
You can fix this by changing your character class [^/.] to just [^/] or - my preferred method - to remove the character class and simply use .+ (seeing as your capturing everything to the end of the string anyway
RewriteRule ^product/([^/]+)/?$ product.php?id=$1 [L]
RewriteRule ^product/(.+)/?$ product.php?id=$1 [L]
Try
RewriteRule ^product/([a-zA-Z0-9_]+)/?$ product.php?id=$1 [L]
I am trying to Rewrite my mode using this info[copy from a website], but it's not working .. what's wrong with it and plz give a correct answer..
The Apache rewrite engine is mainly used to turn dynamic url’s such as www.yoursite.com/product.php?id=123 into static and user friendly url’s such as www.yoursite.com/product/123
RewriteEngine on
RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]
Another example, rewrite from:
www.yoursite.com/script.php?product=123 to www.yoursite.com/cat/product/123/
RewriteRule cat/(.*)/(.*)/$ /script.php?$1=$2
You seem to have misunderstood what the rewrite module (and your rules specifically) actually does.
Quite simply when you browse to:
localhost/abc/product.php?id=23
The RewriteRule isn't invoked and shouldn't be doing anything. There's nothing wrong here, you're just browsing to the wrong URL
URL Transformation
http://www.yoursite.com/product/123 <- URL that user goes to
http://www.yoursite.com/product.php?id=123 <- Rewritten URL that the server sees
RewriteRule(s) explanined
A rewrite rule is broken down into three parts (not including the RewriteRule part...)
The regex that it matches against the url
The url that it transforms into
Additional options
Given your rule:
RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]
The regex is: ^product/([^/.]+)/?$
The new url : product.php?id=$1
The options : [L]
This means that the user browses to the nice url http://www.yoursite.com/product/123 (and all of your links point to the nice URLs) and then the server matches against the regex and transforms it to the new URL that only the server sees.
Effectively, this means that you have two URLs pointing to the same page... The nice URL and the not-nice URL both of which will take you to the same place. The difference is that the not-nice / standard URL is hidden from the general public and others pursuing your site.
Regex
The reason why http://mysite.com/product/image.jpg is not being redirected is because of the regex that you use in your RewriteRule.
Explanation
^product/([^/.]+)/?$
^ => Start of string
product/ => Matches the literal string product/
([^/.]+) => A capture group matching one or more characters up until the next / or .
/?$ => Matches an optional / followed by the end of the string
Given the URL:
http://mysite.com/product/image.jpg
Your regex matches product/image but then it encounters . which stops the matching... As that isn't the end of string $ the rule is invalidated and thus the transform never happens.
You can fix this by changing your character class [^/.] to just [^/] or - my preferred method - to remove the character class and simply use .+ (seeing as your capturing everything to the end of the string anyway
RewriteRule ^product/([^/]+)/?$ product.php?id=$1 [L]
RewriteRule ^product/(.+)/?$ product.php?id=$1 [L]
Try
RewriteRule ^product/([a-zA-Z0-9_]+)/?$ product.php?id=$1 [L]
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 am a complete noob when it comes to the .htaccess file... What I'm trying to do is check to see if a GET variable exists, ?si=10, and pass that onto the rewrite, but only if it exists.
RewriteRule ^user/([^/\.]+)/?$ profile/index.php?name=$1
This way when I have say website.com/user/Username/ it goes to, on the server, website.com/profile/?name=Username instead. But when I add do website.com/user/Account/?si=10, the server isn't passing the si variable onto the actually loaded page. Any idea how I would do this? Sorry if I worded this badly..
Try the QSA flag
RewriteRule ^user/([^/\.]+)/?$ profile/index.php?name=$1 [QSA]
From the manual...
When the replacement URI contains a
query string, the default behavior of
RewriteRule is to discard the existing
query string, and replace it with the
newly generated one. Using the [QSA]
flag causes the query strings to be
combined.
You can do it like this:
RewriteCond %{QUERY_STRING} ^si=([0-9]+)$ // replace regex as neccesary
RewriteRule ^user/([^/\.]+)/?$ profile/index.php?name=$1&si=%1
The percentage sign % brings in the match or matches from the rewriteCond(s) and you use the dollar sign as normal for the matches from the rewriteRule. This gives you full control of your query vars, but yes, QSA is simpler.