convert nginx redirect to .htaccess apache (drupal 7) - apache

I have to redirect url in .htaccess:
original: /mobile/blog/
redirect : /blog/
I tried:
RewriteCond %{REQUEST_URI} ^/mobile/blog/
RewriteRule ^mobile/blog/(.*)$ /blog/$1 [R=301,L]
Without success.
I aslo have these nginx redirect that need to be in .htaccess.
rewrite ^/items/(.+)/(.*) /search/$2 permanent;
rewrite ^/items/(.*) /search/$1 permanent;
rewrite ^/topic/onlyon $scheme://$host/tags/onlyon permanent;
Any help and documentation is appreciated.

Since Drupal sometimes updates the .htaccess file and Drupal often maintains a set of redirect internally it often makes sense to handle redirects using the Redirect module.
That said, if you need or want to do redirects in the .htaccess you can do that just fine. The first redirect you can do without rewrite, you can use Apache's redirect directive:
Redirect permanent /oldlocation /newlocation
To move everything in a directory using rewrite you can use something like this:
RewriteRule ^items(/.*)?$ /search/$1
See also: Apache rewrite rule for whole directory

Related

htaccess 301 redirect not redirecting

tried several ways to make redirect, but not successfull
Redirect 301 /ru/pages/portfolio/ /ru/pages/portfolio/3/ [END,R=301]
or
Redirect 301 http://myssite/en/pages/portfolio http://myssite/en/pages/portfolio/3
and many others from internet, but all of them not working.
Need to redirect pages/portfolio to pages/portfolio/3 (for all languages - en, ru)
This is content of file
<IfModule mod_rewrite.c>
# Turn Off mod_dir Redirect For Existing Directories
DirectorySlash Off
# Rewrite For Public Folder
RewriteEngine on
Redirect 301 /ru/pages/portfolio/ /ru/pages/portfolio/3/ [END,R=301]
RewriteCond $1 !^(pma)
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
Since you are already using mod_rewrite (for an internal rewrite) you should also use mod_rewrite for this redirect, rather than using a mod_alias Redirect. Different modules execute at different times during the request, despite the apparent order of the directives in the config file.
However, your example is unclear. The first example includes a trailing slash; the second does not? Is there a trailing slash or not?
Try something like the following instead after the RewriteEngine directive:
RewriteRule ^(en|ru)/pages/portfolio$ /$1/pages/portfolio/3 [R,L]
This excludes the trailing slash. And assumes "all languages" are just en and ru. This is also a temporary (302) redirect. Change to a permanent (301) redirect (if that is the intention) only when you are sure it's working OK, since 301s are cached by the browser. You will need to clear your browser cache before testing.
Redirect 301 /ru/pages/portfolio/ /ru/pages/portfolio/3/ [END,R=301]
:
Redirect 301 http://myssite/en/pages/portfolio http://myssite/en/pages/portfolio/3
Aside: Neither of these would have worked anyway. End flags like [END,R=301] are a mod_rewrite syntax, and do not relate to mod_alias (Redirect). And the URL-path matched by the Redirect directive should be a root-relative path beginning with a slash, not an absolute URL. See the Apache docs... https://httpd.apache.org/docs/current/mod/mod_alias.html#redirect

htaccess apache rewrite rule

I had a script that ran weather update urls in /weather which is now deleted
These types of urls are 404'ed and need to be redirected to /index.php as I show below
www.atlanticcity.com/weather/index.php?config=&forecast=pass&pass=local_radar&dpp=1&radar_icao=karx&hwvradartype=comp&place=wisconsin+rapids&state=wi&zipcode=&country=us&county=55141&zone=WIZ035
This I tried and doesnt work....
RewriteRule ^weather/(.*) http://www.atlanticcity.com [R=301,L]
You could use a redirect rule instead of rewrite. This would have to go in the apache .conf file under the virtual hosts tag.
Redirect 302 /weather/.* http://www.atlanticcity.com/index.php

Apache mod_rewrite - want a file system redirect not a 301 redirect

I have example1.com on a shared web host running Apache. It has a directory example1.com/foo. I now want example2.com to serve the same content from example1.com/foo, except at the example2.com root without the intervening directory in the URL. Like example2.com/bar.html should serve the same content as example1.com/foo/bar.html .
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule ^(.*)$ foo/$1 [NC]
This simple rewrite rule takes any request intended for example2.com and inserts the foo/ to point to the content which is in that directory. Problem is this keeps doing an external 301 redirect. I don't want that, I want the browser to stay on example2.com without redirecting while Apache serves up the content from /foo in the filesystem.
Been over the Apache mod_rewrite docs several times, which say how to force a 301 redirect with the [R] flag but don't say how to force it NOT to happen. What am I missing here? It is behaving the same on both my Linux shared host and a local test with Apache on Windows.
Thanks!
I figured this out. The 301 was happening because I had the directory name wrong in the rule. So the result of the rule pointed to a path that didn't exist, which makes Apache try to fallback from the file system redirect to a 301 redirect.
Then I had to fix an infinite loop, since that above rule always adds "foo" to the URL even if it's already present so I'd get foo/foo/foo/foo/... . We need to add it only if it's not already there. Had to do it with this two-step rule, because you can't use wildcards in a capturing group of a negative rule. But this seems to work, adding "foo" when the host is example2.com and the URL does not already contain "foo".
RewriteEngine on
RewriteCond %{HTTP_HOST} example2.com$ [NC]
RewriteRule !^foo - [C]
RewriteRule ^(.*)$ foo/$1 [NC,PT]

Redirect all request from old domain to new domain

I am looking to migrate from old domain to new domain.
I have my old domain olddomain.com and new domain newdomain.com pointing to same ip address for now.
I have Apache server inplace to handle requests.
How do I 301 redirect all my
olddomain.com/*
&
www.olddomain.com/*
to
newdomain.com/*
Can I get exact regex or configuration that I need to add in htaccess.
My newdomain.com and olddomain.com both are being serverd by same apache from same IP so "/" redirect might lead to cycles? And so was looking for effecient way
I tried
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$ [OR]
# RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://comp16/$1 [R=301,L]
</IfModule>
And even tried adding in virtual host
RedirectMatch (.*)\.jpg$ http://comp17$1.jpg
But it does not redirect site when i hit localhost in browser to my computer name i.e comp16
In the configuration (VirtualHost) for each of your olddomain.com host try this:
Redirect permanent / http://newdomain.com/
Apache documentation for Redirect. This is the preferred way when everything should be redirected. If you must use mode_rewrite/htaccess there are plenty of questions around this on SO and one of them is:
How do I 301 redirect one domain to the other if the first has a folder path
EDIT
Recommendation from Apache regarding simple redirects:
mod_alias provides the Redirect and RedirectMatch directives, which provide a means to
redirect one URL to another. This kind of simple redirection of one URL, or a class of
URLs, to somewhere else, should be accomplished using these directives rather than
RewriteRule. RedirectMatch allows you to include a regular expression in your
redirection criteria, providing many of the benefits of using RewriteRule.
I also recommend to use an If statement as you can use it also in a multisite server. Just type:
<If "%{HTTP_HOST} == 'old.example.com'">
Redirect "/" "https://new.example.com/"
</If>
Write the below code in to your .htaccess and it will redirect all your old domain request to new domain.
RewriteEngine on
RewriteBase /
RewriteRule (.*) http://newdomain.com/$1 [R=301,L]

Simple rewrite rule for a nginx + apache2 with mod_wsgi

I'm stuck with this, my skills in the web servers area are poor...
I have an Nginx acting as a proxy for an Apache2 running with mod_wsgi and mod_rewrite. What I want to do is rewrite every URL from www.example.com to example.com, i.e. stripping the www part from each URL request before serving. This is the layout of the different conf files:
=== /etc/nginx/sites-enabled/example.com ===:
http://dpaste.com/82638/
=== /etc/apache2/sites-enabled/example.com ===:
http://dpaste.com/hold/82645/
=== /home/nabuco/public_html/example.com/example/apache/example.wsgi ===:
http://dpaste.com/82643/
In my old set up I had an Apache2 running mod_python, and the only thing I had to do was putting an .htaccess file like this:
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
That worked perfectly.
But I tried putting the same .htaccess file into /home/nabuco/public_html/nomadblue.com/nomadblue/apache/.htaccess. If I cast a request without leading www, such as http://example.com/ or http://example.com/whatever, everything goes well. However, if I try the www version of http://www.example.com/ I am redirected to:
http://example.com/example.wsgi/
Do I have to run rewriting rules from nginx instead? I tried that too, adding this to the nginx conf file:
rewrite ^/(.*) http://example.com/$1 permanent;
but now I am getting what firefox calls a "circular loop"...
So who can I get this (I guess trivial) thing up?
Thanks in advance,
Hector
The easiest is to rewrite with nginx. Put that rewrite rule in a dedicated "server" bound to www.example.com
server {
listen 80;
server_name www.example.com;
rewrute ^/(.*) http://example.com/$1 permanent;
}
All right I found the solution to avoid the circular loop... by creating TWO server sections in my nginx config file, one for www.example.com -- which has the rewrite rule suggested by rzab -- and the other for example.com, which contains all the rest of directives.