.htaccess rewrite rule, change URL - .htaccess

I'm looking for the following to change the URL with two variables in a .htaccess file, from:
domain.com/folder/a123/url/index.php
domain.com/otherfolder/a321/url/index.php
to:
domain.com/folder/url/index.php
domain.com/otherfolder/url/index.php
I would prefer it in a single line .htaccess RewriteRule, if possible.
"folder" and "otherfolder" are the only two "folder names" and "a123"/"a321" can be anything starting with "a" and ending with random numbers.

You can do that in one with just one rule using this:
RewriteEngine on
RewriteRule ^(.+)/a[0-9]*/url/index.php$ /$1/url/index.php [R=301,NC]
But if you want to redirect all pages and not only index.php then use this:
RewriteRule ^(.+)/a[0-9]*/url/(.*)$ /$1/url/$2 [R=301,NC]
Also, if you want to rewrite the URLs instead of redirect them change [R=301,NC] to [NC].
Note that if you have that pattern in other URLs this rule will also apply, for example it will also redirect domain.com/adifferentfolder/a567/url/index.php to domain.com/adifferentfolder/url/index.php, if you have those type URLs and don't want to redirect them you need to use two rules instead:
RewriteRule ^folder/a[0-9]*/url/(.*)$ /folder/url/$1 [R=301,NC]
RewriteRule ^otherfolder/a[0-9]*/url/(.*)$ /otherfolder/url/$1 [R=301,NC]

Related

How to redirect url via .htaccess

I want to redirect via .htaccess
https://www.example.co/en/brand/Abc to https://www.example.co/en/brand/abc
I have tried
RewriteRule ^https://www.example.co/en/brand/Abc https://www.example.co/en/brand/abc [R=301,L]
The RewriteRule pattern (1st argument to the RewriteRule directive) matches against the path-part of the URL only, ie. /en/brand/Abc. An additional complication in per-directory .htaccess files is that the URL-path that is matched is also less the directory prefix (which always starts with a slash), so the URL-path does not start with a slash. In other words: en/brand/Abc (for an .htaccess file in the document root).
So, you will need to format the directive like this instead:
RewriteRule ^en/brand/Abc$ https://www.example.co/en/brand/abc [R=301,L]
(Assuming you already have RewriteEngine On defined and that this is near the top of your .htaccess file.)
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
You may try something like this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
# Don't want loops
RewriteCond %{REQUEST_URI} !/abc
RewriteCond %{REQUEST_URI} /Abc
RewriteRule . https://www.example.co/en/brand/abc [R=301,L]
URL are usually case-sensitive. Check this document, while domain names are not. Therefore "abc" and "Abc" are not the same and that's what the question is about. I think.

Redirect index.php with parameters to a folder and remove parameters using htaccess

I have searched but cannot find a specific answer for this exact redirect style...
I have this structure of URL with this specific parameter:
https://websitename.com/directory/index.php?option=com_virtuemart&view=cart
I want it redirected to:
https://websitename.com/shopping-cart/
Note that the above mentioned "directory" changes, but the index.php with the parameters stay the same. No matter what the directory is, I always want it to go to the same exact redirect.
I cannot seem to get the right redirect working in htaccess. Can anyone help?
You can use this redirect rule as your first rule in site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?option=com_virtuemart&view=cart [NC]
RewriteRule ^ /shopping-cart/? [L,R=308]
# remaining rules go below this
You can use a set like this. It takes care on the param view=cart
RewriteCond %{QUERY_STRING} (^|&)view=cart
RewriteRule ^(.*)$ /shopping-cart/? [L,NC,R=301]
If you want to keep the querystring params, then change
/shopping-cart/?
to
/shopping-cart/
without questionmark

How to redirect URLs that do not contain some specific characters by using htaccess file?

Assume that I have some URL like these:
http://mydomaindotcom/abc/123-link.html
http://mydomaindotcom/abc/page/page-link.html
Now I want to redirect all URLs like : http://mydomaindotcom/otherstring/123-link.html to http://mydomaindotcom/abc/123-link.html
or http://mydomaindotcom/anotherone/page/page-link.html to http://mydomaindotcom/abc/page/page-link.html
It means that the first segment on URL must be [abc], if not, redirect to the [abc]-beginning one.
How can I do this?
Try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/abc/ [NC]
RewriteRule ^[^/]+/(.*)$ /abc/$1 [R=301,L,NC,QSA]

htaccess redirects from URLs on one domain to the root of another domain

Currently what is happening is people are accessing old URLs from google like icpaweb.com/site/pages/about-us/ and being sent to their corresponding urls on icpaweb.org : icpaweb.org/site/pages/about-us.
What I want is to send people from: icpaweb.com/site/pages/about-us to icpaweb.org/ without any of the succeeding url segments.
How do I do this?
If you have to use an .htaccess file, you can use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} icpaweb.com$ [NC]
RewriteRule .* http://icpaweb.org/ [R=301,L]
That will 301 redirect all requests for icpaweb.com to the index root of icpaweb.org. If you don't want 301, it can just be R.
You'll need to replace or turn off whatever mechanism is doing your redirecting now, they may not be compatible.
Use an url rewrite rule.
2 steps:
Write a RewriteCond so that the following rewrite rule only apply for url with host being icpaweb.com like RewriteCond %{HTTP_HOST} icpaweb.com$ [NC] The [NC] is for case insensitive match
Write a rewrite rule that convert all input to what you want like RewriteRule ^.*$ http://icpaweb.org/ [L]The [L] is to stop the rewriting to this rule if rule executed.

.htaccess 301 redirect path and all child-paths

I want accesses to e.g. www.thisdomain.com/docs/path1/path2 to redirect to www.thatdomain.com/path1/path2
(Note that docs is not a part of the new path)
I have the following on www.thisdomain.com:
RewriteEngine on
RewriteRule ^docs/* http://www.domain.com/ [R=301,L]
If I access www.thisdomain.com/docs, it directs to www.thatdomain.com, but if I access a child-path like www.thisdomain.com/docs/path1/path2 it fails. Is it possible for the redirect to intercept the child-path access and redirect as I need? If so, any pointers?
Thanks.
With regular expressions, * means any number of the previous atom, which will match /docs and /docs/. Try this:
RewriteEngine on
RewriteRule ^docs$ http://www.domain.com/ [R=301,L,QSA]
RewriteRule ^docs/(.*) http://www.domain.com/$1 [R=301,L,QSA]
(QSA is query string append, so /docs/foo?bar=baz won't lose the ?bar=baz.)
According to section "Redirect Old domain to New domain using htaccess redirect" of the first Google result which I found searching for "htaccess redirect" (without the double quotes), this piece of code will suffice:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
According to their description of the technique, it will redirect the directory the .htaccess file is placed in recursively (including any child paths), just as you intend. Of course, mod_rewrite needs to be present for the rewrite directives to work.

Resources