How to rewrite .aspx to .html or .php with htaccess - asp.net

I'm trying to rewrite some URLs that have a .aspx extension to .html. I looked around and saw that you can put something like
RewriteEngine On
RewriteCond %{QUERY_STRING} ^file=(.+)$
RewriteRule ^(.+)\.aspx$ $1.php?f=%1
in the htaccess to rewrite any .aspx to .php. However, when I put that in the .htaccess file, it doesn't seem to work for me. mod_rewrite is enabled and the site is on a Linux server so those are not the issue. If I try to go anywhere with a .aspx extension, I get a "Server Error in '/' Application." error.
You can see this by going here http://www.netstar.co.uk.php5-20.websitetestlink.com/about-us.aspx. Any help for this would be greatly appreciated...it has been a thorn in my side for quite some time.

You say that the site is on a linux server and mod_rewrite is enabled.
However, when I navigate to the link you provided, I get the following HTTP response headers:
Cache-Control private
Content-Type text/html; charset=utf-8
Server Microsoft-IIS/7.0
X-AspNet-Version 2.0.50727
X-Powered-By ASP.NET
Date Thu, 31 Mar 2011 15:24:33 GMT
Content-Length 1508
Which tells me that no, it's not a Linux server, no it isn't running Apache, and no it doesn't have mod_rewrite enabled.
But I'm not surprised by any of this, because you're writing aspx code, which would normally not be run on a linux/apache server anyway.
I suggest you investigate the IIS equivalent of mod_rewrite. Try starting at this question here on SO: mod_rewrite equivalent for IIS 7.0

My usual mistake: you need to enable .htaccess files in httpd.conf (or, on e.g. Ubuntu, in your site configuration file). In the main <Directory> block (or is it <Location>?) there's probably a directive that ignores .htaccess files in the root directory, or in all directories.
It's probably easiest to just add your rewrite rules to the root <Directory> instead. Remember to restart apache after changing these files.

Related

Redirecting Dynamic URLs with Multiple IDs using .htaccess (Wordpress)

I've just moved my site onto Wordpress, which means the URL structure has now changed. I'd like to use a Rewrite Rule in the .htaccess file to redirect dynamic URLs with multiple IDs in one single statement, but am not having any success. An example is:
OLD URL: example.co.uk/seasons/season.php?ID=1819
NEW URL: example.co.uk/seasons/1819
The statements I have tried in .htaccess are:
RedirectMatch 301 ^/seasons/season.php?ID=(.*).htm$ example.co.uk/seasons/$1
and
RewriteCond %{QUERY_STRING} ^?ID=1$
RewriteRule ^/seasons/season.php$ example.co.uk/seasons/? [R=301,L]
In neither case, the redirect fires. Is there something about a Wordpress .htaccess file that I'm not considering, or is the error with the statements I'm attempting. I'm using .htaccess 301 redirects for http to https and non-www to www without issue.
This is the fixed version of the approach you chose, to make an external redirection:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)ID=(\d+)(?:&|$)
RewriteRule ^/?seasons/season\.php$ https://example.co.uk/seasons/$1 [R=301,QSD]
Since both, the old and new URLs use the same host name you can simplify that:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)ID=(\d+)(?:&|$)
RewriteRule ^/?seasons/season\.php$ /seasons/$1 [R=301,QSD]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
These rules 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).

Can't access file with RewriteRule in .htaccess

I have a folder Storage/static/css. And I want for design/css to see Storage/static/css content.
/Classes
...
...
/Storage
/static
/css
/home
design.css
/menu
/footer
/js
...
/uploads
...
...
index.php
I tried this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -Indexes
</IfModule>
RewriteEngine On
RewriteRule ^design/css/(.*)$ /Storage/static/css/$1
RewriteRule ^ index.php [L]
</IfModule>
For index.php works fine for a long time ago.
But for the other one doesn't work at all.
I want for example:
For design/css/home/design.css to return content from Storage/static/css/home/design.css.
Edit:
My entire web app isn't in root. It is inside a folder called /arshwell.
Looks like you accidentally re-rewrite the css requests in a second go...
Have a try with this slightly modified version:
RewriteEngine On
RewriteRule ^/?design/css/(.*)$ /Storage/static/css/$1 [END]
RewriteRule ^ /index.php [END]
This will prevent that requests rewritten to /Storage/static/css/$1 will immediately get rewritten again to index.php by the next rule.
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule set 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 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).

How can I convert this Apache code to work in NGINX?

I have a file - sitemap.php - which when called shows the same kind of content you might expect from a sitemap.xml file.
I want it so when I go to the URL /sitemap.xml it is actually showing the content that would be showed if you went to /sitemap.php
This used to be achieved by using .htaccess but we don't use Apache now.
RewriteRule ^/sitemap.xml$ sitemap.php [L]
I did try some kind of "apache to nginx" converter thing online but I'm not too fluent in NGINX config code so couldn't say if it was right or wrong.
Sure. Put into your server configuration block before any defined locations this line:
rewrite ^/sitemap.xml$ /sitemap.php last;

Any way to *rewrite* example.com/blog/ URL to blog.otherdomain.com?

I have a Wordpress blog hosted on one server:
http://blog.example2.com/
And another site on a separate server:
http://www.example.com
Is it possible to get the blog to be served at the following URL?:
http://www.example.com/blog/
If so, I'd love to know how. I messed around with mod-rewrite, but it looks like it will only redirect (not rewrite) to another URL, in this case.
For those interested: I realize I could install the blog on the same server, but I'd rather keep things decoupled for now.
Many Thanks
You'll need to use mod_proxy:
ProxyPass /blog http://blog.example2.com/
ProxyPassReverse /blog http://blog.example2.com/
This'll need to be in server/vhost config. Otherwise, you can use it along with mod_rewrite in an htaccess file (in your document root):
RewriteRule ^/?blog/(.*)$ http://blog.example2.com/$1 [L,P]
ProxyPassReverse /blog http://blog.example2.com/
If you're using cookies in your blog, you'll need to make sure to correct the paths/domains.

How do you implement simple website redirects?

If you go to www.codinghorror.com, it is automatically redirected to www.codinghorror.com/blog
How does one re-direct to the "/blog/"?
I'm using Dreamhost Shared Hosting, so my options of configuring the server are limited.
This would be a good opportunity to teach yourself about HTTP status codes. Read the status code definitions section of the HTTP 1.1 spec; section 10.3, "Redirection 3xx," covers the status codes pertinent to your question. Knowing the HTTP 3xx codes means that you can send a response that has exactly the semantics that you intend. For example, if you intend for the redirect to always occur, you could use HTTP 301, "Moved Permanently." In theory, a client could use the HTTP 301 response as a signal to store the new URL value in place of the old one.
Your configuration options at Dreamhost are not as limited as you would think, because you can specify many Apache web server configuration directives in a hidden .htaccess file. This file should be placed in a web document directory; using your example, you would place it in the root web document directory, /home/midas/codinghorror/, though .htaccess files can be placed in any directory served by Apache. (Remember to include the leading dot in the filename.) Its contents would be either the mod_alias example or the mod_rewrite example already mentioned.
Note that mod_alias's RedirectPermanent directive will send an HTTP 301 status code for you. If you wanted to use mod_rewrite to do this, you could specify the status code:
RewriteEngine on
RewriteRule ^$ /blog/ [R=301]
If you use [R] without a code, then HTTP 302 ("Moved Temporarily") is used.
Since PHP is also available to you, that's also an option, though it's possible that the above options using .htaccess are faster. You would place a file called index.php in /home/midas/codinghorror/ and use the code given by Jeremy Ruten above. Again, you can specify a status code in the third argument to header():
<?php
header('Location: http://www.codinghorror.com/blog', TRUE, 301);
?>
Otherwise, using PHP's header() function with 'Location' defaults to sending an HTTP 302 status response.
Another option is using Apache's mod_alias to do a permanent redirect (in either .htaccess or httpd.conf):
RedirectPermanent / /blog
There are probably as many ways as there are servers and programming language (probably even more then that). If you tell us what specific technology you use it can probably help to give you a more specific answer.
Assuming Apache with mod_rewrite and context permissions allowing its use, you put this in virtual host configuration or a .htaccess:
RewriteEngine on
RewriteRule ^$ /blog/
One way is by sending a 'Location' header to the client. Here's a PHP example:
<?php
header('Location: http://www.codinghorror.com/blog');
?>

Resources