Apache redirect - apache

I would like to redirect a URL using RedirectMatch within Apache eg,
/test/one/?? redirect to /test/two/??
where the ?? represents any string that follows
The redirect i'm using below does a straight redirect but doesnt match any string after...
RedirectMatch Permanent ^/test/one?$ /test/two/
Thanks alot

RewriteEngine ON
RewriteBase /
RewriteRule ^/test/one/(.+)$ /test/two/$1
if that does not work, change ^/test/one into ^test/one
make sure mod_rewrite is enabled

You can use mod_rewrite for this:
RewriteEngine On
RewriteBase /
RewriteRule ^/test/one/(.*) /test/two/$1 [L,R=301]
The R flag redirects the page rather than internally rewriting the URI. 301 is the HTTP status code for "Permanently Moved" - if you'd rather use another, you can change it to one of these.

Related

301 redirect (.htaccess) of entire domain to another domain index

I have tried solving this on my own but I can't seem to figure it out.
Here is what I'm trying to do:
Create a 301 redirect of all URLs (including index) on subdomain.example.com to example.com.
So for example:
subdomain.example.com redirects to example.com
subdomain.example.com/blog-post redirects to example.com
This means that I DO NOT want to keep the same URL structure that's used on subdomain.example.com. I just want URLs on the subdomain to point to the index on the example.com. I know that's not the best solution for SEO, but that's how I want it.
I have tried accomplish this using
Redirect 301 / https://example.com/
but that keeps the URL structure and I don't want that.
So how can I solve this?
according to the documentation https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect
keeps the part of the url.
With redirectMatch you can use regexes and substitution to rewrite your url
e.g.
RedirectMatch 301 . https://example.com/
where the dot (.) is a match anything.
You can not achieve this with a Redirect as it appends the old URL path to new one. You can use RewriteRule
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteRule (.*) https://example.com/ [R=301,L]

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 RedirectMatch getting 404 for example.com/choice/

I am trying to do a .htaccess redirect and I have it working, except for one of my cases.
When I have this rule:
RedirectMatch "^/choice$" "/choice/home.html"
The address: www.example.com/choice redirects to www.example.com/choice/home.html.
In contrast when I have this rule:
RedirectMatch "^/choice/$" "/choice/home.html"
And give enter the address: www.example.com/choice/
I get a 404.
I tried this modification of the rule:
RedirectMatch "^/choice\/$" "/choice/home.html"
Recycled the server, but I'm still getting 404.
This is the condition I have set:
RewriteCond %{REQUEST_URI} ^/choice/*
My goal is to have it redirect to the same page whether the final / is present in the address or not. Anyone have any insight? Thanks!
RedirectMatch and RewriteCond have nothing to do with one another. The first is part of mod_alias and the second is part of mod_rewrite - these are two separate modules.
You can accomplish your redirect by using the following:
RewriteEngine on
RewriteRule ^choice/?$ /choice/home.html [R=302,L]
Change 302 to 301 to make the redirect permanent (cached).

htaccess redirect catchall

I need to redirect a directory, and all subdirectories and files in that directory, to the same location (root). So anyone who tries to visit /old, /old/folder, /old/other-folder/xy/page.php, or anywhere else within the 'old' folder, should be redirected to the root domain.
So far, I have this:
Redirect 301 ^/old/.*$ /
Is this the best way of doing it, or would it be better to use (.*) instead of .*? What is the difference between the two?
Or - should I use a RewriteRule instead of a Redirect like above? If so, why?
Thank you!
The Redirect directive doesn't use regular expressions. It connects 2 path nodes together, which isn't exactly what you want. You can try using the RedirectMatch directive instead:
RedirectMatch 301 ^/old/ /
You can try this
RewriteEngine On
RewriteBase /
RewriteRule ^old/?(.*) /$1 [R=301,NC,L]
If you just want to redirect every old page to homepage/root(I'm not sure exactly what you want) than you can replace last rewriterule with
RewriteRule ^old/.* / [R=301,NC,L]

Basic URL rewrite with mod_rewrite

I would like to rewrite the URL of one of my pages from
http://www.mydomain.com/some/application/page.html
to
http://www.mydomain.com/apply
I believe this code will work. But in 301 redirects, you often see [R=301,L] or some version of that appended to the end of the rewrite rule - is the code below the best way to perform the redirection and will Google understand it?
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /some/application/page.html?=$1 [L]
I think you want to do this:
The URL that your users see (even google):
http://www.mydomain.com/apply
to
http://www.mydomain.com/some/application/page.html
the internal(actual) URL.
then I would suggest you to go this:
RewriteEngine On
#condition to redirect
RewriteCond %{REQUEST_URI} ^/?apply/?$
RewriteRule ^/?apply/?$ /some/application/page.html [L]
The flag [L] ( Flag L docs ) signifies RewriteEngine to stop rewriting any rules further. This will not perform a permanent redirection.
You don't need a [R=301] flag here. 301 is for permanent redirection. Use the short URL everywhere.
To my knowledge, you will need [R=301,L]to do a redirect properly. Apache2 by default will use a 302 redirect so if it is a permanent redirect you should force R=301, as you noted. The documentation for RewriteRule is unclear if [L] alone will always perform a 301 redirect. Be safe, tell apache exactly what to do :-).