Nginx URL rewrite to remove folder from URL when its followed by certain subfolders - .htaccess

After I have upgraded my site I see that once I go live with new version some parts of the website URLs will not be redirected for gallery, blogs and files because of new structure. And there is no way fixing it within the CMS. So my goal is to use NGINX redirects.
I wonder do any of you know any NGINX rewrite tricks to make such redirects possible?
website.com/forums/blogs/ into website.com/blogs/
website.com/forums/gallery/ into website.com/gallery/
website.com/forums/files/ into website.com/files/
I actually need the part forums dropped from the URL only and ONLY when the address is going for forums+blogs/gallery/files. Don't want to loose that google traffic.
So for example
website.com/forums/blogs/entry123/my-dog/ is redirected to
website.com/blogs/entry123/my-dog/
BUT
website.com/forums/topic/my-dog/
is left alone and working just like before because the following subfolder is neither blogs or gallery or files.
I needed that once on Apache and this one worked but on Nginx I have no idea.
RewriteRule ^forums/(blogs|gallery|files)/(.*)$ /$1/$2 [L,R=301]

You can try something like
rewrite ^/forums/(blogs|gallery|files)/(.*)$ /$1/$2;
Please note that rewrite directive accepts some flags wich meaning depends on where is it placed (is it inside a server or location block). Detailed documentation is here.

Related

Trying to obtain images from another location but RedirectMatch in htaccess is ignored

I've just setup a new subdomain 'preview', ie preview.example.com and I am setting up a replicate of the codebase of a live website (from the www. subdomain) so that I can have a duplicate environment for testing and showing my client recent changes.
I want to save myself having to copy over all the images to the preview and so the normal location under which images are saved in a WordPress site /wp-content/uploads/ is empty. I wanted to ues a simple htaccess rule to get these instead from the live subdomain where the image at the same path will exist. In other words, a request for
https://preview.example.com/wp-content/uploads/2021/01/test.jpg
would be redirected to the following where the image exists..
https://www.example.com/wp-content/uploads/2021/01/test.jpg
I've tried to set this up with the simplest of htaccess redirect rules but it just seems to be ignored and I've no idea why. Any idea how I can troubleshoot this?
RedirectMatch 301 /wp-content/uploads/(.*) http://www.example.com/wp-content/uploads/$1
Converting my comment to answer so that solution is easy to find for future visitors.
Scenario is that both subdomain and main domains are on same host but their DocumentRoot are set to different paths. OP wants to serve lot of images from subdomain but doesn't to do bulk copy.
Suggested approach without tinkering with .htaccess is to create a symbolic link of wp-content/uploads/. Only option suggested to use in .htaccess is:
Options +FollowSymLinks
Which allows use of symbolic links for serving web requests.

Redirect all pages from old site to my blog homepage in blogger

Old and new site have different structure. Everything is differend.
I don't care about rankinks and SEO, but there are some backlinks that point there and I want to redirect them to the new site homepage. After some months I will delete the whole old site.
I just want a code for .htaccess that redirects any old site url to my new homepage blogger site. I repaeat: To NEW HOMEPAGE, not in related urls.
The old site is hosted as user in my companys domain.
http://users.company.com/myOLDname/
the new one is in google blogger
http://myNEWname.blogspot.com/
Note: If you can help, please put this above url's in the code to better understand.
Thank you.
You need something like that:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^users\.example\.com$
RewriteRule ^/?myOLDname(?:/|$) https://myNEWname.blogspot.com/ [R=301]
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...
Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed 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.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file).

Dealing with Laravel routing without htaccess mod_rewrite capability

So, currently, I do not have the ability to utilize .htaccess, I have no control over the server config at the moment, so I have to live with /index.php/ in my URL. That's not a problem, but there is one issue.
If I go to webserver.com/site/index.php everything works fine. But, if I go to webserver.com/site/ (without index.php) the site loads but all the links are broken (relative to /site/ instead of /site/index.php/).
I've tried various ways to build the links with url() and route() but I can't get anything to work short of hard-coding the full url for every link.
Any ideas?

pointing to a subfolder from a subdomain

My main site www.mysite.com is using drupal
I would like to create a WP blog at blog.mysite.com (I know I can create a blogging platform using drupal but don't wanna do that, WP is best for blogging it simply rocks)
I'm quite new to .htaccess and was wondering if there's a way to get
www.mysite.com/blog pointing to blog.mysite.com (since mysite.com/blog is better for SEO)
(but not redirecting there, it just has to load from there, so visitor sees mysite.com/blog in their browser)
If you have mod_proxy enabled, you can use the [P] flag to internally proxy www.mysite.com/blog requests to blog.mysite.com. In the htaccess file for www.mysite.com's document root (above any drupal related rules):
RewriteEngine On
RewriteRule ^/?blog/(.*)$ http://blog.mysite.com/$1 [L,P]
If you don't have mod_proxy, you may need to setup WP somewhere in the same document root as your drupal site (like in /blog/) and get wordpress to work in a subdirectory. I've had mixed results in getting that to work with another CMS so not sure how much help I can give you there.
gvanto:
Thanks. I don't have mod_proxy but I think it may cause issues having it under a subdomain and accessing in this fashion (e.g. what will all the internal links be pointing to?). The 2nd option of having it in a subfolder is probably best: http://drupal.org/node/877828

IIS URL Rewriting for all Inbound URLs?

Hopefully a simple question although one I have found impossible to answer myself using the Googles!
I have a website on IIS with the URL http://www.contoso.com/ which points to C:\www\public\
There has been a forced directory restructure so now all of the data (Default.aspx, Product.aspx, etc.) that originally resided in C:\www\public\ now resides in C:\www\public\en\ie\ - however, the IIS website document root is still C:\www\public\
So, essentially, I have a lot of inbound links to http://www.contoso.com/Product.aspx?id=1 (etc.) which are now returning 404 errors - the correct link is now http://www.contoso.com/en/ie/Product.aspx?id=1
Please consider that I can make no changes to the directory structure or the IIS document root... so I must solve this issue using URL rewriting.
Is it possible to capture all requests to contoso.com/* and rewrite them to contoso.com/en/ie/* ??
As Ivo suggests, using url rewriting you can set up inbound rules that match the old url pattern and redirect to the new pattern
http://learn.iis.net/page.aspx/460/using-url-rewrite-module/
Change the root directory of your website from C:\www\public\ to C:\www\public\en\ie\ and everything should be fine.
If you cannot do this for any reason, make a custom 404 page and rewrite to url in there and redirect with a 301
See: http://searchengineland.com/url-rewriting-custom-error-pages-in-aspnet-20-12234

Resources