how to do a 301 redirect for all urls in a subdirectory so that they redirect to the particular page but in the root - redirect

how to do a 301 redirect from domain.com/subdirectory/url.htm to domain.com/url.htm so that it redirects all the urls that were previously in the subdirectory but are now in the root directory. I have this
RedirectMatch 301 ^/subdirectory/(.*)$ http://www.domain.com/
But it is redirecting all the pages that were in the subdirectory to the homepage and I want it to redirect to the matching url.
For example redirect
domain.com/subdirectory/blue.htm to domain.com/blue.htm
domain.com/subdirectory/yellow.htm to domain.com/yellow.htm
domain.com/subdirectory/red.htm to domain.com/red.htm
and so on.

Use mod_rewrite:
RewriteEngine on
RewriteRule ^subdirectory/(.*)$ /$1 [R=301,L]
R=301 causes the 301 redirect. L tells the parser to ignore any following rule. You can access the parts matched inside the parentheses by $1, $2, $3 and so forth.

Related

How do I remove slash at the end of the URL on Typo3-9.5.14

We are using Typo3 9.5 on NGINX server. We are experiencing strange problem. When we are accessing a page from our Menubar, it is giving error 404, but if we remove the slash at the end of the page then it is working fine. You can check it here:
http://staging.venicexplorer.net/grids-1/one-column/ - Giving error 404.
http://staging.venicexplorer.net/grids-1/one-column - Working fine.
Can anyone please let us know how could we solve this problem?
Thanks
Prasun
You can add the trailing slash to TYPO3 with the PageType enhancer. See https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/9.5/Feature-86160-PageTypeEnhancerForMappingTypeParameter.html :
routeEnhancers:
AddTrailingSlash:
type: PageType
default: /
index: ''
map:
/: 0
I don't know why the URLs without the trailing slash cause problems on your site. This should not happen out of the box.
In any case, I think it is preferable to have one canonical URL so I would redirect the ones without trailing slash to trailing slash or the other way around.
Apache:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/?typo3/
RewriteRule ^/?(.*)/$ /$1 [L,R=307]
Nginx:
location ~ /?typo3/ {
}
location / {
if (!-e $request_filename){
rewrite ^/?(.*)/$ /$1 redirect;
}
}
Notes:
Please be careful with this and do it on a test site first - internally there is a redirect from /typo3 to /typo3/ - if you redirect all with trailing slash to non trailing slash you can break things. That is why I added the exception in the second line. I do not know if there are other cases on your site where this might break existing URLs.
I always start my rewrites with ^/? - this is only relevant for Apache: This means they will work with starting slash and without - this is due to the fact the passed URLs differ between .htaccess and rules in apache config - the way I do it, the rule will work in both.
You might also be able to do this in TYPO3 - I prefer to do general redirects in the webserver configuration.
Related issue:
Redirect URLs with trailing slashes to their canonical non-trailing-slash URL (or the other way around)

redirect 301 all categories links in wordpress + Redirection plugin

how to redirect all links:
http://www.babynames.ir/esm/oldcat/page1/number/...
to:
http://www.babynames.ir/esm/newcat/page1/number/...
oldcat -> newcat
im using:
Permalink-Finder AND 404 redirect but mess all link.
using Redirection plugin but could not set the parameters...
how about using htaccess like this
add to your .htaccess file after the RewriteEngine On
RedirectMatch 301 http://www.babynames.ir/esm/oldcat/(.*) http://www.babynames.ir/esm/newcat/$1
or may be like this
RewriteRule ^oldcat/?$ $1/newcat$2 [R=301,L]

Redirect root domain homepage to another domain subdirectory

I would like to:
Redirect www.olddomain.com to www.newdomain.com/page
Redirect www.olddomain.com/article1 to www.newdomain.com/article1
Here is what I put into my htaccess file:
redirect 301 / http://www.newdomain.com/page
redirect 301 /article1/ http://www.newdomain.com/article1
The first redirect works, however, the second one does NOT work, since www.olddomain.com/article1 redirects to www.newdomain.com/page/article1, which does not exist and thus returns a 404 error.
How to redirect the old domain home page (root) to a new domain subdirectory, without affecting all other redirects?
In .htaccess code should look like that:
RewriteEngine On
RewriteRule ^/$ http://www.newdomain.com/page [L,R=301]
RewriteRule ^/article1/$ http://www.newdomain.com/article1 [L,R=301]

Joomla after SEF " Page Not Found " 404 Error?

I am trying to open URL of article from Facebook page on my website but the problem is every time error 404 is raised although the article is exists .
After searching I find that there is problem in .htaccess codes and I need help in this.
The problem :
URL : like from facebook page
Website : worked like Real link
As you can see there different between this two links.
The part is added "component/content/" and without the another language prefix "ar".
Some Details :
This is my .htaccess file :
> ##
# #package Joomla
# #copyright Copyright (C) 2005 - 2012 Open Source Matters. All rights reserved.
# #license GNU General Public License version 2 or later; see LICENSE.txt
##
##
# READ THIS COMPLETELY IF YOU CHOOSE TO USE THIS FILE!
#
# The line just below this section: 'Options +FollowSymLinks' may cause problems
# with some server configurations. It is required for use of mod_rewrite, but may already
# be set by your server administrator in a way that dissallows changing it in
# your .htaccess file. If using it causes your server to error out, comment it out (add # to
# beginning of line), reload your site in your browser and test your sef url's. If they work,
# it has been set by your server administrator and you do not need it set here.
##
## Can be commented out if causes errors, see notes above.
Options +FollowSymLinks
## Mod_rewrite in use.
RewriteEngine On
## Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site block out the operations listed below
# This attempts to block the most common type of exploit `attempts` to Joomla!
#
# Block out any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block out any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.
## Begin - Custom redirects
#
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.
#
## End - Custom redirects
##
# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root).
##
RewriteBase /
## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the request is for something within the component folder,
# or for the site root, or for an extensionless URL, or the
# requested URL ends with one of the listed extensions
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
#
# Forward to sitemap
Redirect /sitemap.xml http://www.businesstendersmag.com/ar/?option=com_xmap&view=xml&tmpl=component&id=3
## End - Joomla! core SEF Section.
That's all! Any help will be appreciated.
How do you generate URLs that are posted to Facebook?
Are you generating URLs like this?
// require com_content router.
require_once (JPATH_SITE.'/components/com_content/helpers/route.php');
// generate link.
$link = JURI::root() . substr( JRoute::_( ContentHelperRoute::getArticleRoute( $article->slug, $article->categorySlug ) ), strlen(JURI::base(true)) + 1);
// Where $article->slug & $article->categorySlug are fetched from database.

Nginx rewrite all directory requests to index.php

Okay I'm kind of n00b on Nginx, and I've browsed through here and couldn't piece together an answer. SO here is what i got
server {
root /usr/share/nginx/www;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
if (-f $request_filename) {
expires 30d;
break;
}
if (!-e $request_filename) {
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
location /dojump {
rewrite ^/dojump/(.*)$ /dojump/index.php/$1 break;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
This is on a wordpress setup the first location block should pass all file requests to the wordpress bootstrap.
the location /dojump block is supposed to be for an outbound redirect script i have. I want to catch the arguments and pass them to the index.php script
like /dojump/cnn.com
to /dojump/index.php/cnn.com
it works with apache with this simple .htaccess line inside the dojumps folder
RewriteRule ^(.*)$ index.php/$1 [L]
however, I get an nginx error in the error log
/usr/share/nginx/www/dojump/index.php/cnn.com" failed (20: Not a directory)
Any help?
Thanks
Try passing the url as a GET parameter.
rewrite ^/dojump/(.*)$ /dojump/index.php?url=$1 break;