Apache RewriteRule: preserve Query String and Add new parameters - mod-rewrite

I'm not sure whether this is the origin of the bug I'm trying to solve, however, I'm still not sure I correctly understood Apache doc...
Let's say I wanna redirect
toto.page?a=2&c=546EUE&stuff=blah
to
index.page?view=Blahblah/Toto&a=2&c=546EUE&stuff=blah
Of course, if there was other parameters, they should be preserved too.
Is the following rule correct?
RewriteRule ^toto\.page$ index.page?view=Blahblah/Toto [NC,QSA,L]

Yes. That is correct.

Related

URL Rewrite on the basis of two parameters

i am new to URL Rewrite. i want to rewrite my URL like given below.
www.mywebsite.com/q/1st-parameter/2nd-parameter
to
www.mywebsite/1st-parameter/index.php?q=2nd-parameter
Please note that the 1st-parameter/2nd-parameter is a string and it also contain "-". the Rule which i used in .htaccess file is
RewriteEngine On
RewriteRule ^.+/q/([a-z,A-Z,0-9]+)/([a-z,A-Z,0-9]+) /$1/index.php?q=$2 [NC,L,R]
Your quick reply will be highly appreciated. kindly also look at the Flags, whether i am using the correct flags or not. infact i copied it from a forum but failed to get the correct results.
Thanks in Advance

mod_rewrite to string special characters and shorten URL simultaneously

Gang,
Long time sysadmin but first time poster to this excellent site, so, please be gentle.
I am not strong at REGEX yet and trying to do two things at once on our internally hosted "mediawiki" site.
We are running an otherwise pretty plain jane LAMP stack (centOS 5.x, Apache 2.x, PHP 5.x). We are root. We are using /etc/httpd/conf.d/wiki.conf and not using .htaccess. The physical path is /var/www/html/wiki/
I have partially successful results with some combination of the below, but I am not good enough to get it all the way there. I know that there are some mod_write studs on this site that I am hoping to avail.
I am following this recipe https://www.mediawiki.org/wiki/Manual:Short_URL/Apache so as to shorten URL's from www.example.com/wiki/index.php?=title=Garden_Store to www.example.com/wiki/Garden_Store
still allow the use of www.example.com/wiki/index.php?=title=Garden_Store should the user should choose to type out that syntax of URL. (I believe that is possible with mediawiki to use both style URL's at the same time. If it is impossible, then I will be forced to skip the short URL and use the style with the variable in it.)
Last, string special characters from the URL in the example like www.example.com/wiki/index.php?=title=Garden,_Store! ought to be this www.example.com/wiki/index.php?=title=Garden_Store .
Another example of that might be www.example.com/wiki/index.php?=title=Garden_Store,_Inc. ought to be www.example.com/wiki/index.php?=title=Garden_Store_Inc
One last example, us to make sure that I am communicating well, would be getting this "/title=Garden%20Store,%20Inc" but wanting this "/index.php?title=Garden%20Store%20Inc" as I know that the spaces are replaced with underscores inside of mediawiki.
Thanks so much for walking a newbie the last bit to the finish line on this one.
Cheers.
Jason
Something like the following rules should do what you need:
RewriteRule ^(.*)\ (.*)$ $1\_$2 [L]
RewriteRule ^(.*)[^a-zA-Z0-9\/\._](.*)$ $1$2 [L]
First rule does replace space with underscore and the other line strips all chars you don't want to stay in the resulting URL. Note, your will probably need to add some more, if you want.

(ISAPI_Rewrite) Rewrite domain when certain file is requested

I am trying to create a condition to rewrite a url from http://subdomain.example.com/foo/foofile.txt to http://example.com/foo/foofile.txt when the request has foo/foofile.txt in it. If someone could point me in the right direction, I would appreciate any and all help...
Thanks in advance,
B
This is redirecting a specific subdomain to the main domain, for a specific file.
Which version of isapi_rewrite are you using, v2 or v3?
This assumes v3:
RewriteCond %{HTTP:Host} ^subdomain\.(.*)$
RewriteRule ^(/foo/foofile3\.txt)$ http://%1$1 [NC,R=301]
The RewriteCond tests for a host beginning with subdomain (and a dot), and captures the remaining (example.com) for convenience in the rewrite rule. You could also put the specific example.com in there, but this keeps it more generic.
The RewriteRule then looks for a specific /foo/foofile3.txt, and captures it (again for convenience of not repeating it in the rule). The result has %1 for the capture up in the condition (example.com) and $1 for the capture in the rule (/foo/foofile3.txt)
The NC is not case-sensitive, the R=301 is a permanent redirect.
With v3, querystring parameters are automatically handled, so this keeps any that are after the file.
Other possibilities that make it a little more complex are any subdomain at all, or filename patterns instead of the one specific file.
I was having trouble with the browser caching my attempts while getting the rule right. So even though I changed the rule, the browser had cached my previous redirect. My quick fix was to count up the filename for new urls: foofile2, foofile3 (I guess I got it on the 3rd try...). I could have cleared the browser cache each time too; this was quicker.

mod_speling internal redirect, or how to handle caps with mod_rewrite

I have this rule:
RewriteRule ^(panel|admin|site)/([A-Za-z0-9]+?)/(scripts|assets)/(.*)$ Project/$2/Public/$3/$1/$4 [L]
The idea is to allow access to public assets as they exist inside modules of code. The only issue is that the naming scheme requires title case (let’s assume this is unchangeable). Hence, a request to:
/site/users/scripts/myscript.js
returns 404, but:
/site/Users/scripts/myscript.js
works.
I have attempted to use mod_speling to deal with the casing issue, however mod_speling does a full redirect rather than an internal one, hence the URL to the end user changes to:
/Project/Users/Public/scripts/myscript.js
Which is no good. It means that my directory structure is somewhat exposed and it means I need an extra rewrite rule to pass this through.
Is there any way of handling the capitalisation in mod_rewrite more gracefully, ie without using RewriteMap? (Need to keep everything in the .htaccess)
Alternatively, is there a way of changing mod_speling to use an internal redirect?
handling the capitalisation in mod_rewrite more gracefully, ie without using RewriteMap?
It seems to me that you could use the internal mapping function tolower, and that wouldn't need any external files. Or am I missing something?
Edit: The hacky answer is 26 rewrite rules for each of A-Z as the first letter and the substitution uses the lower case letter... Or a rule per word (like User) that can appear in the URL

Rewrite URL to HTML file causing rewrite to .html/

I'm currently working on an overhaul of my blog site, and have found a way to convert all my current pages into static html pages. They are currently using friendly url's which remap to a central index.php page with GET parameters attached on.
The change I am trying to make is have those same friendly URL's map to their html counterparts. I am currently using this rule:
RewriteRule ^archives?/([^/]+)/([^/.]+)/?$ archives/$1/$2.html
The error log is reporting that it cant find blah.html/ which means it's looking for the .html directory, instead of the .html file. So a better example:
/archives/2009/original-name
should be getting mapped to
/archives/2009/original-name.html
but is really getting mapped to
/archives/2009/original-name.html/
What am I missing here?
don't you need to use it the other way around? I didn't test the code but it should be something like this:
RewriteRule ^archives/(.*)/(.*).html archives/$1/$2
I can't see anything obviously wrong with your regex.
At a guess I'd say you might have a rule somewhere following this, which is redirecting anything without a trailing slash to its equivalent with the slash (a common thing to do to avoid duplicate content issues).
You didn't escape your period in the 2nd statement. Try this.
RewriteRule ^archives?/([^/]+)/([^/\.]+)/?$ archives/$1/$2.html

Resources