Requirement
To configure a optional multi-parameter url pattern in .htacess file.
Issue Description
Parameter values are being truncated as below (check the output url, it's values against the keys [api=item&act=s] )
Input : http://localhost/nFoodApp/items/get
Output URL : /nFoodApp/index.php?api=item&act=s&menu=g&category=e&type=t&item=
Expected Output based on Parameter passed
Input : http://localhost/nFoodApp/items/get
Output URL : /nFoodApp/index.php?api=items&act=get&menu=&category=&type=&item=
OR
Input : nFoodApp/items/get/1
Output URL : /nFoodApp/index.php?api=items&act=get&menu=1&category=&type=&item=
OR
Input : nFoodApp/items/get/1/1
Output URL : /nFoodApp/index.php?api=items&act=get&menu=1&category=1&type=&item=
My .htacess File
RewriteEngine On
RewriteBase /nFoodApp/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/\.]+)/?([^/\.]+)/?([^/]+)/?([^/]+)/?([^/]+)/?(.*)$ index.php?api=$1&act=$2&menu=$3&category=$4&type=$5&item=$6 [L,QSA]
For researchers, the above is the htacess file created after hours of googling, trials and run's with all possible combinations/patterns using *.()[]/$^[L,QSA,R=301,NC], googling, however, could not resolve the issue yet.
Working fine if all parameters passed
Input with all parameter: //localhost/nFoodApp/items/get/1/1/1/1
Output: api=items&act=get&menu=1&category=1&type=1&item=
Might not be able to explain clearly the issue, but the examples clearly state the issue and the config's as shown in the .htaccess snippet.
Thanks in advance
Try to change your last line, using this one:
RewriteRule ^/?([^/.]+)(?:/([^/.]+)(?:/([^/]+)(?:/([^/]+)(?:/([^/]+)(?:/(.*))?)?)?)?)?$ index.php?api=$1&act=$2&menu=$3&category=$4&type=$5&item=$6 [L,QSA]
Related
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.
I am struggling with a .htaccess issue.
I have a url like /files/download/1234/abc/def.pdf where /1234/ is a variable value and changes for each user.
However recently we transferred all the files to /userdownload/abc/def.png
How do i skip the variable level using my .htaccess file?
I tried stuff like:
^files/downloads/(.*)/(.*)$ http://www.example.com/userdownload/$2 [R=301,L]
However this did not give the result i needed as it also removed the /abc/ from the link.
Anyone knows how i can fix this issue?
You can use:
RewriteRule ^files/downloads/(\d+)/(.*)$ http://www.example.com/userdownload/$2 [R=301,L]
If your variable is only numeric.
Or you can use:
RewriteRule ^files/downloads/([^/]+)/(.*)$ http://www.example.com/userdownload/$2 [R=301,L]
So I created a simple mvc project and uploaded In bluehost, but I'm having an server error problem which I guess is because the .htaccess in my project. I've done some research if mod_rewrite is enabled in bluehost, and found that by default it is enabled. But I still get the server error. I hope someone can help me out thanks.
Here my .htaccess inside my public folder:
RewriteEngine On
RewriteBase /streaming/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
Here is the error I get:
server error 500
Your error would indicate that you are trying to perform an operation or function on an array when it is expecting a string. For example, if you have something like:
echo $_GET;
This will not work, because $_GET is an array and the echo command only works with strings and other primitive data types.
Instead, you should either specify the index that you want such as:
echo $_GET['url'];
or if you want all the data in the array you can either loop through each element of the array and process each string one at a time such as:
foreach($_GET as $key => $value){
echo "<p>The value of $key is $value</p>";
}
or use a different method that does support the entire array such as:
print_r($_GET);
Of course this is all guesswork without knowing what your code on line 94 actually says.
With the following url http://www.example.com/de/here/ I want to remove the "de" directory (or whatever may be in front of the "here" directory, if anything even is in front of it) so a user is directed to http://www.example.com/here/ instead, which is a directory that does actually exist.
The url could even be something like http://www.example.com/it/here/ or any other combination of 2 letters.
The url could also just be http://www.example.com/here/ in which case I don't want anything removed at all.
I have searched for a solution here but cant seem to make anything work correctly so any help would be much appreciated.
You can use this kind of htaccess :
RewriteEngine On
RewriteBase /
RewriteRule ^[A-Za-z]{2}/(.*)$ $1 [L,R=301]
Example of redirections caused by this code :
http://www.example.com/de/foo/ => http://www.example.com/foo/
http://www.example.com/de/ => http://www.example.com/
http://www.example.com/it/bar/ => http://www.example.com/bar/
http://www.example.com/FR/baz/ => http://www.example.com/baz/
Please note you won't be able to access the language (de, it, fr...) anymore.
Another point, be careful with this kind of url (the redirection will be executed twice) :
http://www.example.com/de/go/ => http://www.example.com/go/
http://www.example.com/go/ => http://www.example.com/
EDIT
Now I've got more details, here is an htaccess you can you to remove the language for specified folders :
RewriteEngine On
RewriteBase /
RewriteRule ^[A-Za-z]{2}/here/(.*)$ here/$1 [L,R=301]
RewriteRule ^[A-Za-z]{2}/anotherfolder/(.*)$ anotherfolder/$1 [L,R=301]
Problem: Visitors open the url website.com/?i=133r534|213213|12312312 but this url isn't valid anymore and they need to be forwarded to website.com/#Videos:133r534|213213|12312312
What I've tried: During the last hours I tried many mod_rewrite (.htaccess) rules with using Query_String, all failed. The last message in this topic shows a solution for this problem, but what would be the rule in my situation.
I'm very curious how you would solve this problem :)!
The following will handle the simple case you show. You'll need to add additional logic if you need to allow for other parameters in the query string or file names before the ?.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^i=(.*)
RewriteRule ^.* /#Video:%1? [NE,R=permanent]
Why is this tricky?
RewriteRule doesn't look at the query string, so you have to use RewriteCond to evaluate the QUERY_STRING variable and capture the part you'll need later (referenced via %1)
the hash character (#) is normally escaped, you must specify the [NE] flag
The trailing ? on the substitution string is required to suppress the original query string
I tested this on Apache 2.2.