.htaccess RewriteRule with extra url param - .htaccess

Today I have following line in my .htaccess file:
RewriteRule ^test.html default.php?page=10&language=en
I want:
test.html -> default.php?page=10&language=en
(this line is working fine)
And if test.html is called with extra url param then I want to add them, like this:
test.html?user_id=ABC&currency=EUR -> default.php?page=10&language=en&user_id=ABC&currency=EUR
(this line is not working, because I don't get user_id=ABC&currency=EUR)

Use:
RewriteRule ^test.html default.php?page=10&language=en [QSA,L]
With [QSA|qsappend]:
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.
http://httpd.apache.org/docs/current/rewrite/flags.html

Related

How to redirect a txt file to an html page with parameter and then call the content of that file

I'm trying to write a little RewriteRule/RewriteCond but can't find the right resource/tutorial to understand how to achieve my goal. I don't have much knownledge about regex and rewriterules.
That goal is to be able apply a markdown library like Strapdown to a markdown text file when opening it. My idea was :
the user opens the markdown file at http://somedomain.com/myFile.md
RewriteEngine get that you want to do that and redirect you to something like http://somedomain.com/markdown.html?myFile.md
that markdown html page will fetch the content of the markdown file (the filename is in the url parameters) and apply the css library to whatever is loaded and injected in a div.
I think I understood how to redirect the md file to the html document with parameter :
RewriteEngine On
RewriteRule ^(.*).md /markdown.html?md=$1 [QSA,L]
But then the markdown.html page that fetch the .md file is actually fetching the content of markdown.html itself (because of that rule).
So my idea then was to add some character at the end of the filename (like adding a '?' at the end of the file name : "myFile.md?") to be able to catch it in the rewrite rules.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(.*)\?$
RewriteRule ^(.*).md /markdown.html?md=$1 [QSA,L]
But it's not the way to go ... OR my last rewrite synthax is wrong and I don't understand why.
Any ideas ?
Thanks for the help !
I think the issue was that I was trying to use an indetectable pattern (ie : finding '?' at the end of my url).
I changed it to try finding any number as parameter and it works
RewriteEngine On
RewriteCond %{QUERY_STRING} !^([0-9])$
RewriteRule ^(.*).md /markdown.html?md=$1 [QSA,L]
The markdown.html file is now receiving the requested .md file as intended.
By seeing OP's answer I came with some additional improvements in it, please have your htaccess rule file as follows.
RewriteEngine On
RewriteCond %{QUERY_STRING} !^([0-9])$
RewriteRule ^(.*)\.md /markdown.html?md=$1 [QSA,L,NC]
Improvements done in OP's answer:
First escape .md on left side of RewriteRule to make it as a literal character else it may match strings like amd OR bmd etc.
Second use NC flag in your RewriteRule to make it case in-sensitive so that either .md OR .MD both get match.
Also additional suggestion in case your query string could have more than 1 digits in it, change your [0-9] to [0-9]+ in RewriteCond section.

.htaccess not working as expected in sub-folder

I have this rewrite rule placed in /dashboard/.htaccess [dashboard is actually a folder]:
RewriteRule ^([^/]*)$ index.php?mode=$1 [L]
My structure is index.php?mode=support, even though, $_SERVER['QUERY_STRING'] outputs this:
mode=index.php
Example: site.com/dashboard/index.php?mode=support should be site.com/dashboard/support
So , how can I make it parse the param value, and not the file itself.
Managed to solve it while doing more research on regular expressions.
RewriteRule ^([a-z]+)$ index.php?mode=$1 [L,QSA]
Thi solved my problem, preferred plus instead asterisk because it tells the engine to repeat it zero or more times. (when i'm on index.php , query string is empty as needed)
Your rule is matching anything that starts with not a slash and doesnt contain a slash anywhere when your actual path is
/dashboard/support
to get the folder you actually want you need a base on there like this
RewriteBase /dashboard/
If that is placed above the rule, Then your redirect should be ok

htaccess redirect parameter into directory

I setup a slightly weird hierarchy where index reads the URI to decide the action instead of using actual subdirectories, example: site.com/base/action/
Im moving over from the old system to this new one, but I cant get the redirects to work. What I need is: /base/?param[]=value to do an external redirect to /base/param/value/
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.+?)(\[\])?=([^&]+)&?(.*)$
RewriteRule ^/?base/?$ /base/%1/%3/?%4 [L,R=301]
This will also preserve any query string that may be after param[]=value. You can alternatively just clobber the entire query string altogether by removing the %4 at the end of the rule's target.

Why does my RewriteRule not work when there is a `?` in the URL

I am learning how to write regular expressions for .htaccess redirects.
So far I've managed to figure out everything I needed, except for a couple of regular expressions which don't behave as I expected. I am testing my regular expressions using a desktop application, and they work fine there, but not in the .htaccess file.
FYI: The RewriteBase is set to /site/
This is the incoming URL:
/site/view-by-tag/politics/?el_mcal_month=3&el_mcal_year=2009
I want to grab "politics" and redirect to /site/tags/politics/
Here is what I used:
RewriteRule ^view-by-tag/([a-zA-Z\-]+)/([a-zA-Z0-9\-\/\.\_\=\?\&]+) /tags/$1/ [R=301,L]
I added the capture of all the characters after politics because I am having the issue that when there is a ? in the URL the redirect does not work, and I can't figure out why. In the URL given above, if I remove the ? it works fine, but if the ? is in there, nothing happens. Is there a reason for this?
The same thing happens when I try to capture 307 from /site/?option=com_content&view=article&id=307&catid=89&Itemid=55
I used this regular expression, article&id=([0-9]+) /?p=$1 [R=301,L] but again, when there is a ? in the URL it stops the redirect for doing anything.
What is the reason for that?
The .htaccess file in question is on a Wordpress blog (3.4.1)
The point that you've missed is that the rewrite engine splits the URI into two parts: the REQUEST_URI and the QUERY_STRING. The query string part isn't used in the rule match string so there is no point in constructing rule regexp patterns to look for it.
You can probe and pick out parameters from the query string by using rewrite conditions and condition regexps to set %N variables.
By default the query string is appended to the output substitution string unless you have a ?someparam in it -- in which case it is ignored unless you used the [QSA] (query string append) parameter.
The way that you'd pick up the id in /site/?option=com_content&view=article&id=307&catid=89&Itemid=55 is to use something like:
RewriteCond %{QUERY_STRING} \bid=(\d+)
Before the rule and this would set %1 to 307. Read the rewrite documentation for more general discussion of how to do this.
The query string is must be processed separately in a RewriteCond if you need to manipulate it, and should not be matched inside the RewriteRule Instead, just match the request not including the query string, and use QSA to append the query string onto the redirect:
RewriteRule ^view-by-tag/([A-Za-z-]+)/?$ /tags/$1/ [R=301,L,QSA]
# OR, if you don't want the rest of the query string appended, put a `?` onto
# the redirect to replace it with nothing
RewriteRule ^view-by-tag/([A-Za-z-]+)/?$ /tags/$1/? [R=301,L]
Actually, the QSA may not be needed in a R redirect - I think that the default behavior is to pass the query string with the redirect.
If you need to capture 307 from the query string, do it in a RewriteCond and capture in %1:
# Capture the id in %1
RewriteCond %{QUERY_STRING} id=([\d]+)
# Redirect everything to /, pass %1 into p
RewriteRule . /?p=%1 [LR=301,L]

.htaccess issue with redirecting folder

I have a folder named /test in my application.
Right now i am trying to write an .htaccess file that would show all requests to /test* as /test.
For example:
www.example.com/test/ is the actual directory with index.php file in it.
All the requests like the following should go to the same /test directory
www.example.com/test-hello/
www.example.com/test-world/
www.example.com/test-htacess/
www.example.com/test123/
Basically any requests to /test* should go to /test.
This is what I've tried so far:
RewriteRule ^/test* /test
You need to use RewriteCond to first match "test in url"
Try below:
RewriteCond %{THE_REQUEST} ^GET\ /test/
RewriteRule ^test/(.*) /test/$1 [L,R=301]
Your regular expression is wrong. You mean ^/test.*$. Your rule would match to /testtttt.
The asterisk means that the char in front of it can be zero or more times included. The dot is a special char which means here could be anything. the .* matches every string including an empty string. See also Wikipedia.
You currently are not putting the -hello, -world etc behind your folder. What is hello? Is that the file? Or the param?
The second part of the rewriteRule should be a file. Something like
RewriteRule ^/test(.*)$ /test/$1.php
Above function will have:
/testABC to /test/ABC.php
But I don't understand what you want to accomplish?

Resources