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

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).

Related

NGINX Rewrite Rule without access to the configuration file

In apache, the rewrite rule can be written in the configuration file or in .htaccess file. How about in nginx? Can I use url rewriting without access to the configuration file?
Unfortunately, you can't. This is one of the reasons shared hostings typically use apache or litespeed, not nginx or lighttpd.
A (very ugly) workaround would be to handle all requests with a script which would contain the rewrite rules and would serve the file/script according to the request URI (and which could be modified by a user without having root privileges). However you'd have a bad performance serving static files and you'd need to handle all the request headers by this script, which is not very practical.

Symfony 2 - What to do before going live?

Until now, I've been writing Symfony 2 applications and never was responsible for launches. So I've only called Smyfony 2 over app_dev.php and only used a development configuration. Now I have to take care of a Symfony 2 launch, what do consider before going live?
Where can I set different configurations (DB connection, etc.) for different environments and how can I invoke them?
How can I make sure the application can't be called via app_dev.php on the live server?
And of cure any other tips on what to keep a eye on while pushing a Symfony 2 application live.
First of all, we have documented this: http://symfony.com/doc/current/cookbook/deployment-tools.html That article just answers the first and third question, the answer on the second question:
How can I make sure the application can't be called via app_dev.php on the live server?
You should point the root directory of you website to the web directory. For instance, if you site lives in:
mysite.com/
public_html/
... your site files/directories
You should make it like this:
mysite.com/
app/
...
src/
...
vendor/
...
public_html/ <--- this is the old web directory
...
app.php
You can read how to change the web directory in public_html into the documentation too: http://symfony.com/doc/current/cookbook/configuration/override_dir_structure.html
The reason to do this is to secure all your application code. The user just can't access all app/., vendor/. and src/. files and so it can't read the configuration and can't read the parameters.
At last, you can change app.php to index.php to get urls like mysite.com/app.php/blog/foobar. But it is better to create a simple HTACCESS mod_rewrite rule to send all requests to app.php, so you can access the page with mysite.com/blog/foobar.
.htaccess in the root:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*?)$ app.php$1
Where can I set different configurations (DB connection, etc.) for different environments and how can I invoke them?
if you haven't changed too much your project, your config files are under app/config
How can I make sure the application can't be called via app_dev.php on the live server?
Same, if you haven't change to much the project, the only thing you need to care about is make sure your web server runs the website from /web (the .htaccess does the rest)
And of cure any other tips on what to keep a eye on while pushing a Symfony 2 application live.
You can keep an eye on the log files in app/logs

Removing URL file extension

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.

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.

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