I am migrating site and bringing all my product urls to top level.
Example:
Old URL: http://www.example.com/sub/folder/product.asp
Old URL: http://www.example.com/sub1/folder1/product.asp
New URL: http://www.example.com/product.asp
I am struggling to get the nginx rewrite rule to work how i need it. Basically anything that ends in *.asp need to strip out all the folder paths.
You need to identify URIs ending with .asp that include more than one /. One possible solution might be:
rewrite ^/.+(/[^/]+\.asp)$ $1 permanent;
See this document for details.
Related
I am trying to achieve the following redirect. A migrated website had images orginally hosted in subfolders with (to the new site) unknown/random subfolders, like this:
/artikel_images/2938/30_01.jpg
/artikel_images/4050/20_11.jpg
/artikel_images/1291/image.jpg
On the new website all images are uploaded/hosted in:
/wp-content/uploads/2023/01/
Returning in the following result:
/wp-content/uploads/2023/01/30_01.jpg
/wp-content/uploads/2023/01/20_11.jpg
/wp-content/uploads/2023/01/image.jpg
I am trying to create a nginx redirect for all possible origin links (that are still in the content) to the single new link.
Hope to get some help, thanks!
I have been trying with several regular expressions, like below to achieve the redirect.
Edit: with thanks to Richard this is the solution:
location /artikel_images { rewrite ^/artikel_images/[0-9]+/(.+)$ /wp-content/uploads/2023/01/$1 last; }
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/;
i just moved my server from apache to nginx, and my main site from joomla to wordpress (now it is a lot faster, but it cost me two months of learning nginx, and testing new configs etc.). Only problem that left is with new links structure.
Can somebody help me to rewrite old links to the new one?
This is mine links structure:
OLD> http://www.example.com/this-is-just-some-text-1234.html
NEW > https://www.example.com/this-is-just-some-text-09878
Main text in most cases stays the same, but post-id number is changed, and there is no .html at the end. http > https redirects are in nginx config already.
This can't really be done with nginx alone -- how would nginx have any idea what was the old ID with Joomla, and the new one with WordPress? It sounds like your transition process wasn't done correctly.
However, if you do have a list of old and corresponding new URLs, you can use the the map directive within nginx to supply such list.
map $uri $new_uri {
/this-is-just-some-text-1234.html /this-is-just-some-text-09878;
}
if ($new_uri) {
return 301 $new_uri;
}
References:
http://nginx.org/r/map
http://nginx.org/r/if
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.
I am caching my websites pages into a static flat file cache and am trying to use a try_files directive to load the file first from cache if it exists and then go to web application.
I'm basically trying to do something like this:
try_files $uri $uri/ /var/www/vhosts/example.com/httpdocs/staticfilecache/$uri/index.html
In this example if the user requested example.com/products/ it would try staticfilecache/products/index.html The problem I have is that $uri already contains a trailing slash. Trying to use this with $uriindex.html creates an error.
Is there any performant/easy way to always strip the / but not rewrite the address in the browser?
What you are trying to do has many problems on many levels.
"/var/www/vhosts/$uri" is invalid. Except if you have duplicated
your whole document root under the staticfilecache folder.
Nginx has it's own caching system and will check it first by itself
without any intervention from you as long as it has been setup.
Nginx will check for index files by itself as long as the index directive has been set.
Your code is actually attempting to check the staticcache last and
not first as you said you wanted.
There is no point in creating a static file cache on disk. Nginx can
just as well read the static files from their original locations.
In any case, Nginx will create the cache for items that need cache,
usually dynamic files, by itself. It is not a manual job.
Basically, it looks like you are a bit on the wrong track on some webserver fundamentals.
The answer(strictly) to your question is,
rewrite ^(.*)/$ $1 break;
But I would recommend you don't do this and go through #Dayo's answer.
For some interesting stuff for caching guidance, you can check this out.