Rewriting RewriteRule to include back one particular php file in the path - regex

I inherited a large WordPress site: I was looking at the Wordfence live logs when I found users not loading a particular PHP page. I investigated and through an FTP client I found the file was where it was supposed to be. I used some network tool (in Chrome, Opera and Firefox) and, again, I found that file was returning a 404.
So, I found in the root of the website a short htaccess file containing this line:
RewriteRule ^wp-content/(.*)\.php$ [R=404,L]
I commented this out and reloaded the website: no error anymore. I must say, the error apparently doesn't cause anything strange to the website. But I would like to eliminate it.
I suppose this rule is meant to avoid someone can make a direct HTTP request to this and any other PHP file in that directory: in this case I suppose this file I'm talking about is called from an include, not directly, because in WordFence what I see is an error coming after a user accesses directly other pages, not this one in particular.
Anyway, I would like to rewrite this rule so that it stays the same as now, except for that php page. The PHP page is in the path of the theme:
wp-content/themes/themeName/core/css/customized.css.php
Is this possible? Any help is appreciated

If you want to exclude that specific php file from the RewriteRule, you can add a negative lookahead to the regex, like so:
RewriteRule ^wp-content/(?!themes/.*/core/css/customized\.css\.php$)(.*)\.php$ [R=404,L]

Related

301 redirect to correct url

I have a lot of incorrect bad links and want to 301 redirect them to the correct one, the correct url are as follows:
Blockquote http://www.domain.com/string-video_string.html
the back links are pointing to:
Blockquote http://www.domain.com/string_string.html
any possible way to 301 redirect the wrong back links to the correct links?
Thank you in adance
You can use this rule in your site root .htaccess:
RedirectMatch 301 ^/([^_-]+)_(.+)$ /$1-video_$2
Depending on how you want to redirect (in which method; PHP, htaccess, etc.) you have some options.
I assume you're seeing 404 errors when users are trying to get to the links from an external source, like a search engine.
If that's the case, you can easily generate the code you need for which ever method you choose using this website:
http://www.rapidtables.com/web/tools/redirect-generator.htm
Make sure that you correctly format the URL's you want to redirect and it should work fine.
If you want to make sure your SEO issues get fixed, you should create a robots.txt file and place it in the root directory of your site (usually where the index file is) - and follow the instructions from this site: http://tools.seobook.com/robots-txt/ to de-index the bad links from the search engine. You may also want to create and submit (or resubmit) XML site maps to the search engines your users use most.

How do i change the base url of my application from : http://test.something.com/ to http://test.something.com/abc.cfm

The url : http://test.something.com/ is the login page of the application. By default it calls a page 'login.cfm'. But it doesnt shows in the url. May be it is defined in the server file as a default page.
What I want is, when i access this url [http://test.something.com/], the url should also show the called page i.e. the url should look like : http://test.something.com/login.cfm everytime.
I dumped the cgi variables and below is the stack for the same. I was wondering whether i have to change the cgi.http_referer or cgi.http_host.
Or do I need to change some file on the server side? I am using Apache.
EDITED:
what you are asking might be a browser dependent issue. In the Browser there is option to trim the URL from the address bar, which might be the case for you.
Please check this link and get it confirmed.
http://www.cnet.com/how-to/how-to-show-the-full-url-in-firefox/
Still if you want to change your application's URL to look something else then you can try "URL rewrite"
You can check this link for URL rewrite.
http://www.iis.net/learn/extensions/url-rewrite-module/user-friendly-url-rule-template
(You forgot to show the cgi scope dump, fyi)
Are you certain that Application.cfc/Application.cfm isn't just showinng the login page? Also look in any header files. That's the likely scenario always showing the login page.
Anyway, you can use .htaccess to do just this but it's not clear whether you want each subdirectory to its index.cfm, or if you want to redirect to the root.
If you want the redirect to the index page of the current directory (/something/ redirects to /something/index.cfm)
RewriteCond %{REQUEST_URI} !\..*$
RewriteRule ^((.*)$) /$1/index.cfm [L,R=301]
If, instead, you want it to redirect to the root folder, that's a pretty simple change
RewriteCond %{REQUEST_URI} !\..*$
RewriteRule ^((.*)$) /index.cfm [L,R=301]
The L in both examples stops processing of rules after that, so if you have other rules in .htaccess, that may be a concern.
The R flag (R=301) is important to cause the URL to visibly change, creating the effect you want.

When using URL rewrite files are included multiple times?

I am developing a new site in ColdFusion 10 and doing some url rewriting using the .htaccess file. I have three rules that work very well when I call them without other includes. When I wrap them in includes, I get an error.
Here are my rules:
RewriteEngine on
RewriteRule ^advertise$ /index.cfm?Section=Advertise
RewriteRule ^bike/([0-9]+)/([0-9]+)/ /index.cfm?Section=Bike&BikeID=$1&FeatureID=$2
RewriteRule ^bike/([0-9]+)/ /index.cfm?Section=Bike&BikeID=$1
The index page figures our which section to show and calls includes the appropriate file wrapped around a header and footer, like this:
include "header.cfm";
include "#VARIABLES.Section#.cfm";
include "footer.cfm";
The problem is, it doesn't quite work for the "Bike" pages, the second two rules. It appears that the header file is included several times, which makes the links to the CSS and other files bad, which messes up the page. If I comment out the header and footer includes, everything works just fine, but there's no header or footer, which is not what I want.
// include "header.cfm";
include "#VARIABLES.Section#.cfm";
// include "footer.cfm";
Is there something in the url rewrite rules that causes the bike pages to be loaded several times and then fail?
http://flyingpiston2012-com.securec37.ezhostingserver.com/
You can try the page here and see the problem by clicking the links. Any ideas on how to fix this?
UPDATE
When I make all of my links absolute by including the entire URL, the problem goes away.
<cfoutput>
<link href="#APP.Home#/bootstrap/css/bootstrap.css" rel="stylesheet">
</cfoutput>
Still, I should NOT have to include an absolute path to files. Any hints as to why this isn't working for me?
UPDATE
// THIS FIXES THE PROBLEM
<base href="#APP.Home#/">
<link href="/bootstrap/css/bootstrap.css" rel="stylesheet">
// THIS FIXES THE PROBLEM
<link href="#APP.Home#/bootstrap/css/bootstrap.css" rel="stylesheet">
// THIS IS THE PROBLEM
<link href="/bootstrap/css/bootstrap.css" rel="stylesheet">
Whatever your problem is, trying to make a connection between .htaccess rewrite rules and files included by CF is the wrong troubleshooting approach.
Rewrite rules are applied only to the incoming request: the document requested in the address bar (for example) of the browser.
When one includes a file, it's not making a request, so the webserver (and accordingly the rewrite module) are not involved.
I imagine if you simply browse to the rewritten URL for the bikes page (eg: /index.cfm?Section-Bije&BikeID=12345) you'd see the same problem. This demonstrates the rewriting is not part of the problem.
Factor this notion out of your investigation, and just look at how your files are being included. A good simple place to start is to just turn debugging on, and look at the execution time dispay using the TREE view. This'll give you an idea of where/how/why you're calling in your templates multiple times.
This could happen because you didn't tell Apache to stop parsing on match. Try to make rules look like this:
RewriteRule ^advertise$ /index.cfm?Section=Advertise [L]
RewriteRule ^bike/([0-9]+)/([0-9]+)/ /index.cfm?Section=Bike&BikeID=$1&FeatureID=$2 [L]
RewriteRule ^bike/([0-9]+)/ /index.cfm?Section=Bike&BikeID=$1 [L]
I don't know if it is related, but I did run into a similar issue with FW/1. By tailing the rewrite.log I could see every cfinclude that did NOT actually exist was actually going through Apache. I added [NS] to the .htaccess and it went away:
# rewrite rules, NS skips rewrite rule for internal sub-requests
# for FW/1, removes index.cfm from the URL
RewriteRule ^(.*)$ /index.cfm/$1 [NS,L]

With Coldfusion, how do you handle dynamicaly generated URLs?

(Update: I converted this question to a community wiki as the answer appears more subjective than I thought it would. There are multiple answers depending on one's needs.)
If I have a folder that only includes application.cfc and index.cfm, what is a fast, reliable method to handle dynamically generated URLs? i.e. URLs that do not have a corresponding physical .cfm file.
This example url generates a 404, but it should lookup a page in a db and return it via index.cfm:
http://www.myserver.com/cfdemo/mynewpage.cfm
Should I use onMissingTemplate() in the application.cfc to handle the missing file? Since this method doesn't process onRequestStart(), onRequest() and onRequestEnd(), I wonder if it should be avoided.
Alternately, I could setup an ISAPIRewrite rule since I'm using IIS (or mod_rewrite on Apache)
# IF the request is not /index.cfm, doesn't exist and ends in cfm or html,
# rewrite it. Pass the requested filename $1.$2 as the 1st param: cgi.page
# append the remaining url params $4 ($3 is the ?)
RewriteCond %{SCRIPT_NAME} ^(?!/index.cfm)(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^\/(.*)\.(cfm|html)(\??)(.*)$ /index.cfm?page=$1.$2&$4 [I,L]
Are these methods appropriate, or am I missing a better way of accomplishing this goal? It seems that Coldfusion should have this type of feature built into the application.cfc. Maybe I'm just missing it.
nothing wrong with url rewrite on web server level. I'd vote for that.
Because CF by default handles only cfm/cfc requests, you can do in the beginning of Application.cfc something like this:
<cfif Right(cgi.SCRIPT_NAME, 9) NEQ "index.cfm">
<!--- analyze the SCRIPT_NAME and start processing --->
</cfif>
For other filetypes using web-server configuration is the only way I can see. But instead of creating rewriting rules you can try to use custom 404 handlers. At least when using IIS you'll be able to get the context in cgi.QUERY_STRING, if set up the dummy page, say 404.cfm (it does not need to exist) and putting following check before previous example:
<!--- trap 404 requests triggered by IIS --->
<cfif right(cgi.SCRIPT_NAME, 7) EQ "404.cfm">
<cflog file="mylogfile" text="404 error triggered by IIS. Context: #cgi.QUERY_STRING#">
</cfif>
For Apache it is possible to use following handler, but I'm not sure if you can extract the context in this case:
ErrorDocument 404 /404.cfm
If you are doing this for SES URLs, I'd offer two pieces of advice.
The first is that they matter less and less as time goes on. Google, for example, recognizes that URLs need to include query data.
Second: CF can natively handle SES URLs in the form hostname/file.cfm/param1/param2. Ray Camden's BlogCFC, for example, works that way. It is on by default in CF8, but needs to be enabled in CF7. I don't have a lot of information handy on this, but it should be easy to Google (or Bing, or whatever).
If you can allow it, I'd try to convert URLs like:
http://www.myserver.com/cfdemo/mynewpage.cfm
to:
http://www.myserver.com/cfdemo/mynewpage OR
http://www.myserver.com/index.cfm/cfdemo/mynewpage
so that you don't lose the onRequest methods. The first one can be done only at the webserver level, so in Apache or IIS. The second one can be done in just ColdFusion. See this: http://www.cfcdeveloper.com/index.cfm/2007/4/7/Coldfusion-SES-URL.
Otherwise, if you must have the .cfm at the end, you can use a URL rewrite package in Apache or IIS to strip it out and then forward the request to a cfm page or do what you're doing with onMissingTemplate. I'd try to opt for a solution that doesn't involve losing the onRequest methods, but up to you.
I'd definitely go for URL rewriting. Not only will it be a more predictable, yet generalized approach, but it reduces a significant amount of string parsing load from the CF server. Further, it results in CF handling a request to a real file thereby getting you the benefit of onapplicationstart, onrequeststart, and other events.
As an aside, I've personally always found URLs like /index.cfm/foo/bar/ to look unpro and hackish. Additionally, URLs (like /foo/bar) that don't end in either a file extension or trailing slash are technically incorrect (per old-school static site conventions at the very least) and ought to probably be avoided as well. I'd also be curious where Ben Doom gets his assertion that "The first is that they matter less and less as time goes on. Google, for example, recognizes that URLs need to include query data." In my experience I've actually found the exact opposite to be true.

Codeigniter Routes for filename with extension

I am using codeigniter and its routes system successfully with some lovely regexp, however I have come unstuck on what should be an easy peasy thing in the system.
I want to include a bunch of search engine related files (for Google webmaster etc.) plus the robots.txt file, all in a controller.
So, I have create the controller and updated the routes file and don't seem to be able to get it working with these files.
Here's a snip from my routes file:
$route['robots\.txt|LiveSearchSiteAuth\.xml'] = 'search_controller/files';
Within the function I use the URI helper to figure out which content to show.
Now I can't get this to match, which points to my regexp being wrong. I'm sure this is a really obvious one but its late and my caffeine tank is empty :)
You should not need to escape the full stop, CodeIgniter does most of the escaping for you.
Here is a working example I use:
$route['news/rss/all.rss'] = "news/rss";
Issue was actually in .htaccess file where I had created a rewrite exception to allow the search engine files to be accessed directly rather than routing them through codeigniter.
RewriteCond $1 !^(index\.php|google421b29fc254592e0.html|LiveSearchSiteAuth.xml|content|robots\.txt|favicon.ico)
Became
RewriteCond $1 !^(index\.php|content|favicon.ico)