301 Redirect of Static HTML to Dynamic PHP Page - http-status-code-301

After upgrading our website, many old links that people have in blogs, etc. are now going to our 404 error page.
An example is: (using h#p b/c I'm a new user and can't post links)
h#p://www.site.com/pressreleases/some_release.html
h#p://www.site.com/pressreleases/another_release.html
These items are now part of a db-driven site and would be live here:
h#p://www.site.com/pressreleases/details.php?id=1
h#p://www.site.com/pressreleases/details.php?id=2
How can I set up the 301 to redirect
h#p://www.site.com/pressreleases/some_release.html
to
h#p://www.site.com/pressreleases/details.php?id=1,
and
h#p://www.site.com/pressreleases/another_release.html
to
h#p://www.site.com/pressreleases/details.php?id=2?
Thanks

Compose a .htaccess file in the pressreleases directory and specify the following:
Redirect 301 some_release.html details.php?id=1
If you would like to redirect using regular expressions, use mod_rewrite as explained here.
There are various options listed on this page.

If you have a lot of these URLs, and assuming you have access to the Apache config, consider creating a "redirects.inc" file in /etc/apache2 (or anywhere really) and then adding "include /etc/apache2/redirects.inc" to your virtual host. That way you have one place to add/update your redirects.

Related

Htaccess rules: How to redirect to specificp page only if content inside a folder doesn't exist

I have this issue (I also searched through many threads but couldn't find the answer).
I moved a lot of content from principal domain to archives.
The action called firstaction used to have this link:
https://www.sitename.com/actions/firstaction (wordpress site)
and now it moved to
https://archives.sitename.it/actions/firstaction (totally different wordpress site).
But I still have a lot of content under www.sitename.com/actions/
So I want to redirect only if the content doesn't exist.
Only content under /actions/ folder.
I guess the redirect action could be something like
RedirectMatch "^/actions/(.*)" "https://archives.sitename.eu/actions/$1"
But I don't know how to add all the conditions I need.
I probably need to use the Skip flag S, but this goes beyond my abilities.
Thanks for your help!
p.s. it's a wordpress site under apache server.

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.

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

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.

mysite.com/new redirects to mysite.com

I just rebuilt my site to be responsive with Gantry 5 and Joomla. built in inmotion hosting server. moved it into a sub domain on my real sites server, www.umiultrasound.com/new. when i try to access the site to test it redirects to www.umiultrasound.com.
I have discussed this with my host, they say i have a wildcard redirect- i don't. they say to check my .htaccess file. i did but i do not see where i am redirecting / or www.mysite.com to itself.
they then recommended that i read one of your forum results. the one they sent didn't apply to me. hoping you can help
Check for any RewriteBase command in .htaccess. You could also temporarily copy the standard htaccess.txt over .htaccess to see if this makes any difference (backup .htaccess to .htaccess.old or similar first).
Also check your configuration.php file to see if the live_site parameter has been set.

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