What to do here for mod_rewrite? - .htaccess

This question haven't been answered completely and a part of this question: What's the code should behind this situation?
The un-answered part is here with a little edit, please I need your help. Thanks in advance for your help.
I have:
forum.php?fid=1&p=2
thread.php?tid=1&p=2
( the numerous values are not only 1 or 2, its ([0-9]+) )
How to do it like this:
forum/f1/anything/page-2
forum/t1/anything/page-2
For more:
All of my pages of the script is formatted like this: any.php?xid=n&p=n ( n != 1 or n > 1 ).
So i want the same prefix used for
any.php, that related to the any.php, for i.e. any.php has a link to book.php, so I want the url like this:
any/a1/anything/page-2
any/b1/anything/page-2
( One more thing, page-n will be shown when n > 1, so it needs to be worked if it is: any/a1/anything. Please help me, I am stuck here. )

Try the following:
RewriteEngine On
RewriteRule ^forum/f(\d+)/[^/]+(?:/page-(\d+))?/?$ /forum.php?fid=$1&p=$2 [L]
RewriteRule ^forum/t(\d+)/[^/]+(?:/page-(\d+))?/?$ /thread.php?tid=$1&p=$2 [L]
Please, note that any/a1/anything is being redirected as any.php?aid=1&p= i.e. p would come but without a value. For all practical purposes (and since the redirected URL is never made visible to the client) it shouldn't matter.

Related

Need help rewriting "Transp 0 : 100"

as everyone probably knows "Transp = is being deprecated"
I am aware of how to rewrite color.new and recode that part.
However, in a different section of a pine script I see transp = lines? 0 : 100 which in the script then allows the color and slider bar to be manipulated.
Can anyone help point me to where I can find how to rewrite this portion of the pine-script?
Thank you in advance
here are images of how the old and new code show up (old is colorful and optional, new is B/W)
transp == lines ? color.new(color.blue,100) : color.new(color.blue,10)
Its all about the condition, if it's true then trans will be 100, if not then Tran will be 10. Change trans to your liking
You can post full code and I'll correct if you not able to do it

How to make apache treat query string as file name?

I mirrored a site to local server with wget and the file names locally look like this:
comments
comments?id=123
Locally these are static files that show unique content.
But when I access second file in browser it keeps showing content from file comments and appends the query string to it ?id=123 so it is not showing content from file comments?id=123
It loads the correct file if I manually encode the ? TO %3F in browser window and I type:
comments%3Fid=123
Is there a way to fix this ? Maybe make apache stop treating ? as query separator and treat it as file name character ? Or make an URL rewrite and change ? into %3F ?
Edit: Indeed too many problems caused by ? in file name and requests. I ended up using the wget option --restrict-file-names=windows that would convert ? into an # when saving file name.
The short answer is "don't do that."
The longer answer is that ? is a reserved character in URLs, using it as a part of a filename is going to cause problems forever, and the recommended solution is to pick a different character to use in those filenames. There are many to choose from - just avoid ? & # and # and you'll probably be fine.
If you insist on keeping the file name (or if you don't have an option) try:
RewriteCond %{QUERY_STRING} (.*)
RewriteRule (.*) $1%%3F%1 [NE]
However, this is going to fire any time you have a query string, which is likely not what you want.

.htaccess condition that works on many conditions inside

I want to try something like if in .htaccess:
I want to Redictes each ?sp=SOMEWHAT to diffrent ?p=NNN (some number)
I have a 100 ?sp= pages.
And I don't want to work on 100 Rules each page load.
If this another method to solve it, I happy to know.
if(RewriteCond %{HTTP_HOST ^?sp=}{
RewriteRule ^?sp=bar ?p=5
RewriteRule ^?sp=foo ?p=9
RewriteRule ^?sp=tin ?p=15
}
This is no logic between the ?sp= and ?p=
Update: I doesn't have access to server config.
This can be done with the RewriteMap directive (iff you have access to the server configuration, as pointed out in a comment. No idea why they thought that needed to be restricted...). For example:
RewriteMap sp_to_s txt:/path/to/map.txt
RewriteRule ^?sp=(.*) ?p=${sp_to_s:$1|0}
(the 0 is the default value if none of the pairs in the map match).
Here's a sample map.txt:
bar 5
foo 9
tin 15
There are more ways to use the map feature; see the documentation for mod_rewrite for details.

URLs with symbol "%" at the end make http error, how to prevent it with htaccess?

I have a doubt with some of my URLs from my acces_log . There are some URLs from external sites linking me like http://domain.com/url_name.htm% (yes, with %).
Then... my server returns http error, I need to redirect this fake URLs to the correct way, and I thought in htaccess.
I only need to detect the % symbol in the last character of URL, and redirect without it.
http://domain.com/url_name.htm% --> http://domain.com/url_name.htm
How can I do this? I was trying with some samples with ? symbol but I didn't have lucky.
Thanks!
I already found the mistake...
It seems that some malformed URLs don't pass to vhost, then these petitions don't read the .htaccess.
The only way to solve this, is adding in httpd.conf the ErrorDocument 400 directive... Not is the best option for servers with different vhosts.. because all of the will have the same behaviour... but I think that is the only way for this case.
Quotation from Apache documentation:
Although most error messages can be overriden, there are certain circumstances where the >internal messages are used regardless of the setting of ErrorDocument. In particular, if a >malformed request is detected, normal request processing will be immediately halted and the >internal error message returned. This is necessary to guard against security problems >caused by bad requests.
Thanks anyway!!
This page is super helpful about the .htaccess rules.
http://www.helicontech.com/isapi_rewrite/doc/RewriteRule.htm
I saw a few solutions to this that use a small php script too. IE this one replaces #
.htaccess
RewriteRule old.php redirect.php? url=http://example.com/new.php|hash [R=301,QSA,L]
redirect.php
<?php
$new_url = str_replace("|", "#", $_GET['url']);
header("Location: ".$new_url, 301);
die;
?>

.HTACCESS REWRITE

Hi I need to rewrite this :
http://www.xxx.co.uk/holidays/resort/courchevel/chalet/chalet-xx/pricing.html
to this:
http://www.xxx.co.uk/holidays/courchevel/chalet-xx/pricing.html
though the chalet name and resort changes so i would need the $1 and $2 in there i think
thanks in advance for any help
If i've understood your question correctly, something along these lines should do it (based on your edit)...
^holidays/([^/]+)/([^/]+)/pricing.html /holidays/resort/$1/chalet/$2/pricing.html

Resources