Removing URL file extension - asp.net

I have been exploring "prettier" URLs of which the first thing I've done is remove the extension (eg. .aspx). To do this I've used the URL Rewrite module in IIS.
This works so now if I access
http://www.mysite.com/folder/filename
or
http://www.mysite.com/folder/filename.aspx
The latter gets redirected to the first and the page loads. I then tried the following:
http://www.mysite.com/folder/filename/
which breaks (as expected I suppose) but this seems like bad user experience. Maybe as a frequent web user I feel that having no slash or a slash should work. The only way I know to have a slash on the end that works is to create a folder and use the default default.aspx page, which I'd like to avoid if I can.
Continuing, I tried adding a directory with the same name as a file, so in the case of the example above I created a directory called filename. In this directory I created a default default.aspx. Now, if I try and access the same url http://www.mysite.com/folder/filename I am sent to the default.aspx page of that new folder with a slash appended to the URL http://www.mysite.com/folder/filename/.
This suggests that by default URLs without extensions attempt to access directories, and only if not found will IIS overwrite provide a file, so in a sense it seems like a hack to try and fool it with extension-less file names.
I am fortunately in a position where I have full control over the file and folder names, so I can hopefully prevent any accidents there (albeit I will have to be careful). So my question: Is there, a way to remove the file extension but still allow a trailing slash to be appended and still find the file. I don't want to have to create loads of directories all with default.aspx inside, if I can help it.

I'm not completely sure how IIS handles mod_rewrite but hopefully the logic that i apply in a linux environment will help you solve the problem.
In my .htaccess file i first write two conditions that will apply to the following rules; it says that if the following url is not a file and is not a directory redirect the user to the following page.
However if the directory or the file exists that will always take precedence.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^news/([^/]*)$ news.php?article_slug=$1 [L,QSA]
The rule allows for the structure you require (folder/file) or at least simulates it as a url.
So the above example will direct
http://www.mysite.com/news/article-name
&
http://www.mysite.com/news/article-name/
to news.php with a GET variable article_slug
Let me know if thats of any help or if i can help further.

Easiest way is to use ASP.NET routing, I presume that you are talking about web forms, for your example it's pretty easy:
Add System.Web.Routing reference to the project
Use routing in Global.asax.cs (using System.Web.Routing;)
Add routes in application start :
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("", "Filename", "~/Filename.aspx");
}
and that's it.
Here is a more information about Routing for win forms :
http://msdn.microsoft.com/en-us/library/cc668201.aspx#adding_routes_to_a_web_forms_application
http://msdn.microsoft.com/en-us/library/dd329551.aspx

IIS Rewrite (correctly) matches URL's with a trailing slash and without a trailing slash as different URL's. Depending on how the rule has been implemented, you may need to
implement 2 rules, one to deal with adding (or removing) trailing slashes from all or sepcific URLS and the other to deal with
rewriting URL's to .aspx pages.
you might need to added separate maps for URL's with and without
trailing slashes
The solution really depends on where and how you match.

Related

Is there a way loading an existing page under a fake url?

I want to rewrite an existing path page to a fake directory.
www.website.com/company/events -> fake path, it doesn't exist but it should load /events/ page.
www.website.com/events
Is there a way to do it in Wordpress?
This is what I tried in .htaccess but it does't work.
RewriteCond %{REQUEST_URI} ^/events/ [NC]
RewriteRule .* /company/events/ [L]
P.S: I want to mention the /events/ page is generated by a WP plugin called Events Calendar.
Thanks!
Sure that is possible:
RewriteEngine on
RewriteRule ^/?company/events/?$ /events
An alternative would be an alias:
Alias /company/events /events
You obviously need to integrate this with the wordpress generated rewriting rules, something we cannot really help with. Especially since you did not post those...
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

nginx redirect map folder

I can't seem to get something to work to redirect anything from a folder or its subfolders to another folder in my nginx redirect map.
I want to redirect any request to anything within the folder /fauxnews to /topics/faux-news. (Not to redirect to another file with the same name in the destination folder, but just to "/topics/faux-news", which will list all posts in that topic.)
I've found things that seemingly should work, like:
/fauxnews(.*) /topics/faux-news/;
/fauxnews.* /topics/faux-news/;
... but they aren't working. What should I use there?
Okay, found out you have to tell nginx you're using regex for a particular line, by inserting the tilde at the beginning. (The '^' is an anchor telling it to start the matching there):
~^/fauxnews(.*) /topics/faux-news/;

In a framework using rewrite rules, how best to integrate CSS and JS?

I've got a framework that routes all incoming URIs through a base file, and to deal with static files I've got a sub-directory /static in which I put all CSS, JS and images (ie. /static/css/main.css) in order to keep things clear.
My own code and plugins deal with this fine, but some times other code needs to be implemented, and often CSS files will try to style up with calls to URIs in those files. How can I deal with this in the best way?
An example ;
/about/company
routes to
/script?q=about/company
and locks in the main structure of the site. However;
/static/css/main.css
uses a background image from;
/static/images/widget/bg-color.png
Since this is a framework I'm not happy to hard-code the /static paths in the CSS files. For one, I don't want to restrict websites to only being served from some root directory. :) For all JS there's objects that deal with this (ie. var x = $xs_dir.js + '/script.js' ;) but nothing exists for CSS. I have five options, I think ;
(worst) Have an option in my admin tool that scans all CSS files for URI references, and prepends them with the right static directory, and writing all CSS as if they're static to a root directory.
(poor) Rely on the webserver's ability to alias any static directory to one root static directory, and let the admins deal with it.
(meh, slow) Serve the CSS files through the framework, filtering URIs with the right static paths.
(simplest, but not very easy) Hand-code the static portions of my CSS files for whatever server setup there might be, and just make sure they're easy to find and change.
(probably best, but complex?) Have a rewrite rule that detects images in current directory, forwarding them to the static directory, and write all CSS with some recognized dynamic path. (ie. instead of /static/images/img.png do images/img.png and rely on rewrite rules to push it where it needs to go, also restricting the website structure to never have a sub-directory called 'images')
Any additional options? Ideas? I know Joomla and similar has some rewriting of files, and probably do no. 5?
Not sure whether you have an .htaccess file or not, but one option would be to put in a rewrite rule for request under a folder "images" to be redirected to your static (or whatever) folder:
RewriteRule ^[^\?]*(images/)(.*)$ /static/$1$2 [NC,L]
That way you can reference all your images as just /images/whatever.png in your CSS.
If you require a different setup for different servers, you could always have a separate .htaccess file for each environment or server, and version control these.
You can use the HTML base tag http://www.w3schools.com/tags/tag_base.asp to set the path for all relative url in your HTML. However, this might break all your relative link too.
It is probably a bit optimistic to demand this approach from admins, particularly if they don't control the server environment, however:
You can do this by the use of a separate (sub)domain to serve static files. You can then create a separate virtual host in your web server to serve these files. This approach also have the advantage that you can optimize for speed by using a cookie-less domain, and aggressive cache options. In addition, it allows the browser to fire up additional threads against the sub-domain, to download these files in parallel with the rest of the page.
E.G. Google is already hosting a number of open source frameworks which you can use directly, like Jquery UI with the corresponding css.
Another option for admins is to use a reverse proxy, like varnish, to serve and cache any existing file, and then pass every other get uri to the backend server containing framework controller (then the framework files, except for 'router', should be outside of the public folder for security).
I guess that at least having the option to serve static files from a cookie-less domain would be handy, else Duffmanss answer should suffice.
Assuming you're routing everything to 'script.php' this would route all non-existing files to script.php, thus allowing static content to be served directly, while routing to your controllers as well.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ script.php [QSA,L]
If all the CSS files end up in /static/css/ and all the images which the CSS references end up in /static/images/, then just make sure that whenever you reference an image from CSS that you use a relative path ../images/widget/bg-color.png and it should work fine.
Option 5 works with Apache. If all your image references in your css files start with a relative subdir without a preceding slash (eg. url(images/image.png)), then in <css_dir> you can rewrite images/ to <where_the_images_are>. Your .htaccess stays in, and only applies to <css_dir> along with the css files and work from wherever you dropped it.
The RewriteRule only needs to know that images/ relative to itself is the one that matters.

Does WordPress do something that overrides .htaccess?

I'm working locally on a WordPress site for a client. The client keeps their install of wordpress in the server root, but because I have more than one client, I like to install everything in a client folder, like localhost/client. Normally it's a simple matter to add a rewrite rule to .htaccess, setting the rewrite base to be /client/. But today it's not working at all. Even if I put nonsense in the rewrite rule, the site works (which it should not, if the rewrite rules are in effect). The images references are still looking for root at localhost/, instead of localhost/client/, which means they are broken.
I looked into WordPress's documentation and found it has something called WP_rewrite, and there is a place to set parameters for it in wp-includes/rewrite.php). I set the root parameter to client/ but that doesn't seem to have any effect either.
One last thing: there is an .htaccess file in localhost/client/, and another one in localhost/client/wp/. I have them both set to RewriteBase = /client/, but I've tried just about every other sensible combination--no change.
So my question is: does WordPress do something else that would make the rewrite rules I set in .htaccess not work?
I'd recommend a small change to your local environment for a more flexible solution. Use virtual hosts. If you're running WAMP/MAMP, this tool is readily available to you. It allows you run a directory within your localhost as its own site, which you can access using a local-only URL of your choosing. I have my sites set up as sitename.local, for instance, so they all live in their own happy little ecosystem. Highly recommended.
Yes it does, you have to make some steps that will change entries in db before you move it to subfolder.
Here are detailed info:
http://codex.wordpress.org/Moving_WordPress#Moving_WordPress_Within_Your_Site
What you need to do:
copy everything as it is setup on your clients server i.e. in root folder
do the steps in above url, important are 4. & 5.; step 10. actually just go to permalink settings and click on "Save" button (or "Update" I do not remember how it says)
Now you can work :)
Are you modifying only theme or what?

Using IIS6, how can I place files in a sub-folder but have them served as if they were in the root?

Our ASP.NET 3.5 website running on IIS 6 has two teams that are adding content:
Development team adding code.
Business team adding simple web pages.
For sanity and organization, we would like for the business team to add their web pages to a sub-folder in the project:
Root: for pages of development team
Content: for pages of business team
But
We would like for users to be able to navigate to the business team content without having to append "Content" in their URLs, as described below:
Root: Default.aspx (Available at: www.oursite.com/default.aspx)
Content: Popcorn.aspx (Available at: www.oursite.com/popcorn.aspx)
Is there a way we can accomplish for making a config entry in an ISAPI rewrite tool for every one of these pages?
Since the extensions will be ASPX, ASP.NET will pick up the request... you can write an HttpModule that checks for pages that yield a 404 and then check the subfolder also.
If you know that all pages with a certain format will be coming from that folder, then you can just rewrite the URL in ASP.NET (either in Global.asax or an HttpModule).
I don't have any way to test this right now, but I think you can use the -f flag on RewriteCond to check if a file exists, in either directory.
RewriteCond %{REQUEST_FILENAME} -!f
RewriteCond Content/%{REQUEST_FILENAME} -f
RewriteRule (.*) Content/(.*)
Something like that might do what you're after, too.

Resources