How to include a single $ (dollar-sign) within the rewrite rule - apache

I'm trying to create a rewrite rule for my .htaccess file. I want to include a single dollar sign within it. It is supposed to be something like this:
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^\$$ "http\:\/\/domain\.com\/something" [R=301,L]
The problem is that it doesn't work with a single dollar sign, so if I go to domain.com/$ I get a 404. It works only with another letter, for example ^\$a$ - in such case domain.com/$a would redirect to domain.com/something.
The workaround is to create a new folder, rename it to $ and put the .htaccess file there, but I would rather avoid creating multiple folders with no content for such purposes. I couldn't find any reference on the Internet (the official Apache documentation for mod_rewrite was not very helpful). I tried using multiple slashes in different combinations but everything failed. Am I missing something or is it just impossible to make it work this way?

For me it works exactly as expected in htaccess with RewriteRule ^\$$ and requesting "$". Have you looked at the rewritelog / loglevel rewrite:trace8 yet?

Related

Apache 2.4 .htaccess friendly URL rewrite

Having some issues understanding the .htaccess file and getting it to work properly. .htaccess is recognized....i entered plain test at the top and got the internal server error. And it appears mod-rewrite is working...because I downloaded a test php file and it works.
I am using Apache 2.4.23 with no php pages
However i cant seem to get this to work right.
I am trying to take this url
http://example.com/ProjectTest/index.shtml?dynContent=Content1
and rewrite it like this
http://example.com/ProjectTest/Content1
I am also trying to do this generically across the site since ?dynContent=whatever will be a constant.....and trying to keep the same path.....like the below example
Change this
http://example.com/ProjectTest/ProjectFolder/index.shtml?dynContent=Content1
to this
http://example.com/ProjectTest/ProjectFolder/Content1
I am not very good with the .htaccess files
UPDATE: Ok...I got this to work.....not really sure why it works....but when I type in the test-dev/ProjectTest/apples or test-dev/ProjectTest/oranges it calls up the appropriate content.
This is what I used
RewriteRule ^ProjectTest/([^/\.]+)/?$ ProjectTest/index.shtml?dynContent=$1 [L]
and then I rewrote it to be this and now it works for any directory
RewriteRule ^(.*)/([^/\.]+)/?$ $1/index.shtml?dynContent=$2 [L]
Try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^dynContent=(.+)$
RewriteRule ^/(.+)+/index.shtml $1/%1 [PT]
This requires mod_rewrite to be activated. The value of the bracket in dynContent=(.+) is stored in %1 and the bracket's value in ^/(.+)+/index.shtml is stored in $1. Both together creates your target path http://example.com/ProjectTest/ProjectFolder/Content1

Apache - mod_rewrite how to prefer files instead of directories (if both have same name)?

I have very similar problem like this one: Apache mod_rewrite - prefer files over directories with pretty URLs
However it is not same, and solutions mentioned in above link doesn't work for me.
My directory structure looks like this:
/pages/articles/january.php
/pages/articles.php
/pages/home.php
/articles/
/index.php
Now, I am including in index.php pages (depending on url).
For example, when user types address www.domain.com (or www.domain.com/home), index.php will include /pages/home.php
But if I enter this URL: www.domain.com/articles it will make link something like this: www.domain.com/articles/?page[]=articles (in other words index.php won't include /pages/articles.php file)
On the other hand, this works perfectly: www.domain.com/articles/january
This is my htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9+]*)/([a-zA-Z0-9+]*)$ %{DOCUMENT_ROOT}/index.php?page[]=$1&page[]=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9+]*)$ %{DOCUMENT_ROOT}/index.php?page[]=$1 [QSA,L]
I use array for page because I can have subpages (and subpages work fine!).
EDIT: I have found this however it doesn't solves my problem :(
Can someone tell me why it does this and how to fix it?
Or, how can I give priority to files instead directories?
EDIT 2: I solved it by removing "/articles/" directory, however I am still interested how to make it work through htaccess file rules.
Per the comments above, your original issue was worked around by removing the articles directory, but you still wanted to be able to deal with directories in that sort of situation.
You'd probably want to split into two sets of rules. Have an earlier rule that uses the -d flag in a RewriteCond to catch directories so that it could treat them differently as needed. Alternately, ignore directories in the first rule by negating the flag (!-d) then catch them in the later rule.

multiple folder redirect

I have been trying variations of the following without success:
Redirect permanent /([0-9]+)/([0-9]+)/(.?).html http://example.com/($3)
It seems to have no effect. I have also tried rewrite with similar lack of results.
I want all links similar to: http://example.com/2002/10/some-long-title.html
to redirect the browser and spiders to: http://example.com/some-long-title
When I save this to my server, and visit a link with the nested folders, it just returns a 404 with the original URL unchanged in the address bar. What I want is the new location in the address bar (and the content of course).
I guess this is more or less what you are looking for:
RewriteEngine On
ReriteRule ^/([0-9]+)/([0-9]+)/(.?)\.html$ http://example.com/$3 [L,R=301]
This can be used inside the central apache configuration. If you have to use .htaccess files because you don't have access to the apache configuration then the syntax is slightly different.
Using mod_alias, you want the RedirectMatch, not the regular Redirect directive:
RedirectMatch permanent ^/([0-9]+)/([0-9]+)/(.+)\.html$ http://example.com/$3
Your last grouping needs to be (.+) which means anything that's 1 character or more, what you had before, (.?) matches anything that is either 0 or 1 character. Also, the last backreference doesn't need the parentheses.
Using mod_rewrite, it looks similar:
RewriteEngine On
RewriteRule ^/([0-9]+)/([0-9]+)/(.+)\.html$ http://example.com/$3 [L,R=301]

How do I rewrite www.sitename.com/thing/thing.php?otherthing=something-like-this to www.sitename.com/something-like-this?

How do I rewrite
www.sitename.com/thing/thing.php?otherthing=something-like-this
to
www.sitename.com/something-like-this?
please help me with this as I can't seem to succeed. My host uses apache 2.2. Many thanks for your help!
Update
No I don't need that trailing ? However, I used the Rewrite rule you offered me and it still ain't working. I also added a RewriteEngine On before the rules.
I have Linux hosting, .htaccess and the code is obviously semantically correct, cause otherwise I would get the all so popular 500 internal server error. I placed the .htaccess file in the folder thing and in the root of the site, but it still won't work.
There should be an option to display it in directory format instead of the PHP ? format. If not, you could use the .htaccess mod_rewrite rule to make that display in the /folder/ way.
The way I do it is that I just upload my files and each page name is index.html and then I create folders, and put each index.html in the folder. Like this:
/guidelines/
In that folder is index.html, so instead of it being /guidelines.html it's /guidelines/
Looks better without .html
You need to use mod_rewrite:
RewriteCond %{QUERY_STRING} ^otherthing=(.*)$
RewriteRule ^thing/thing.php$ /%1? [L]
No idea if you meant to have that trailing ? at the end of the rewrite, I don't think that's possible. Note that the ? at the end of the RewriteRule is to get rid of the query string, otherwise, the rewritten URL will still have the ?otherthing=something-like-this at the end.

mod_rewrite to alias one file suffix type to another

I hope I can explain this clearly enough, but if not let me know and I'll try to clarify.
I'm currently developing a site using ColdFusion and have a mod_rewrite rule in place to make it look like the site is using PHP. Any requests for index.php get processed by index.cfm (the rule maps *.php to *.cfm).
This works great - so far, so good. The problem is that I want to return a 404 status code if index.cfm (or any ColdFusion page) is requested directly.
If I try to block access to *.cfm files using mod_rewrite it also returns a 404 for requests to *.php.
I figure I might have to change my Apache config rather than use .htaccess
You can use the S flag to skip the 404 rule, like this:
RewriteEngine on
# Do not separate these two rules so long as the first has S=1
RewriteRule (.*)\.php$ $1.cfm [S=1]
RewriteRule \.cfm$ - [R=404]
If you are also using the Alias option then you should also add the PT flag. See the mod_rewrite documentation for details.
Post the rules you already have as a starting point so people don't have to recreate it to help you.
I would suggest testing [L] on the rule that maps .php to .cfm files as the first thing to try.
You have to use two distinct groups of rewrite rules, one for .php, the other for .chm and make them mutually exclusives with RewriteCond %{REQUEST_FILENAME}. And make use of the flag [L] as suggested by jj33.
You can keep your rules in .htaccess.