Remove URI segment using htaccess - .htaccess

Here is my url:
http://localhost/BA/cookies-policy/register
I want that page to direct to :
http://localhost/BA/register
...and so on if the above link is accessed.
I am not familiar with htaccess.

Try this in .htaccess file in root directory:
RewriteEngine on
RewriteBase /
RewriteRule ^/BA/(.+)$ /$1 [L,QSA]

Basic .htaccess 301 redirect rule for one specific URL:
Redirect 301 /BA/cookies-policy/register http://localhost/BA/register
Rewrite rule to match your needs:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^BA/(.*)$ /$1 [R=301,L]

Try it like below, in your BA directory.
RewriteEngine On
RewriteRule ^cookies-policy/(.+?)$ /BA/$1 [R=301,L]

Related

htaccess 301 redirect to index page

I need the sloution for home page 301 redirection.
If I enter http://www.starmed.dk/index.php in the browse bar, then it will be redirected to http://www.starmed.dk without index.php
Any idea how to do this with an HTACCESS 301 redirect?
Thanks in advance.
//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^starmed.dk[nc]
RewriteRule ^(.*)$ http://www.starmed.dk/$1 [r=301,nc]
//301 Redirect Old File
Redirect 301 www.starmed.dk/index.php www.starmed.dk
Edit:
Perhaps your configuration is different. perhaps this:
RewriteRule ^www.starmed.dk/index.php$ www.starmed.dk/ [R=301]
Try the following :
DirectoryIndexRedirect Off
RewriteEngine on
RewriteRule ^index\.php$ / [L,R]

Cannot 301 redirect this page to a specific page

I want this page:
http://mystore.com/Login.aspx?ReturnUrl=%2fPages%2fHome%2fUser%2fWish-list.aspx
to
http://mystore.com/en/ukeurope/home
So, I try to write a 301 redirection rule like this:
RewriteRule ^Login.aspx$ en/ukeurope/home? [R=301,L]
but when I want to try this redirection.
It gives me 404 not Found.
P.S. I don't want another one that is
mystore.com/Page/Login.aspx
to be redirected by this 301 redirection rule
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+Login\.aspx?ReturnUrl=\%2fPages\%2fHome\%2fUser\%2fWish-list\.aspx [NC]
RewriteRule ^ /en/ukeurope/home? [R=302,L]

.htaccess redirect FROM subfolder to domain name

I had website content in a subfolder (http://mydomain.com/subfolder/index.php), now I copied everything over to the root folder (http://mydomain.com/index.php).
I would like to redirect all the visitors that have bookmarked the old page to the new content (at least to the new index.php) using .htaccess.
Is this correct:
RewriteEngine on
RewriteRule /subfolder/^(.*)$ http://mydomain.com [R=301,L]
?
And where would I place the .htaccess file, in the subfolder or the root folder?
Placing the following .htaccess in / (where your index.php is located) should do the trick:
RewriteEngine on
RewriteRule ^subfolder/(.*)$ /$1 [R=301,L]
Or you could place the following .htaccess in /subfolder:
RewriteEngine On
RewriteRule ^(.*)$ /$1 [R=301,L]
Note that the () around .* and the $1 redirects /subfolder/someFile.php to /someFile.php. If you skip it, everything in /subfolder redirects to /.
Thanks to #mariusnn in the comments, I was able to solve this issue.
❌ Not Working: .htaccess redirect FROM subfolder to domain name
RewriteEngine on
RewriteRule ^subfolder/(.*)$ /$1 [R=301,L]
✅ Working: .htaccess redirect entire subdomain "/subdomain/" and files "/subdomain/file_01.php" within are redirected to domain name "example.com"
RewriteEngine on
RewriteRule ^subfolder/.*$ / [R=301,L]
*Note that the () around .* and the $1 redirects /subfolder/someFile.php to /someFile.php. If you skip it, everything in /subfolder redirects to /.
This does the trick:
RedirectMatch 301 ^/subfolder$ http://yourdomain.tld/
Try:
RewriteEngine On
RewriteRule ^subfolder/index.php$ /index.php[NC,L,R]
Tried all answers here. This is what worked for me:
RewriteEngine on
RewriteBase /
RewriteRule ^subfolder/(.*)$ /$1 [R=302,NC,L]
Change R=302 to R=301 after you've validated the changes.

Rewrite all files from folder to another folder

I want redirect all requested files from folder-a to folder-b. E.g. http://www.yoursite.com/folder-a/index.php to http://www.yoursite.com/folder-b/index.php.
How can I do that? I have verified if mod_rewrite works with this code:
RewriteEngine On
RewriteRule ^google.html$ http://www.google.com/ [R=301]
The structure on the webspace is the following:
.htacces
|-folder-a
|-folder-b
But if I want my folder redirect
RewriteEngine On
RewriteRule ^/folder-a/(.*)$ http://www.yoursite.com/folder-b/$1 [L,R=301]
the redirect doesn't work if I input the following URL:
http://www.yoursite.com/folder-a/
http://www.yoursite.com/folder-a/index.php
The redirect doesn't take place. I stay on the same page ... What I'm doing wrong? I also tried it with this htaccess:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^folder-a(.*)$ http://www.yoursite.com/folder-b$1 [L,R=301]
</IfModule>
If all this works I want to exclude some files. Eg. with this htaccess:
RedirectPermanent /folder-a/info.php /folder-b/new-info.php
Edit:
Now I tried this htaccess
redirectMatch 301 ^/folder-a/ http://www.yoursite.com/folder-b
This works, but I need something which takes the whole path and rewrite it to the new folder.
This for example doesn't work:
RewriteRule ^folder-a/(.*)$ folder-b/$1
Solution
I had an old htacess file in my folder-a so the redirect didn't worked. This is my final htaccess:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^folder-a/excludefile1.php http://www.yoursite.com/folder-b/newnameforfile1.php [L,R=301]
RewriteRule ^folder-a/excludefile2.php http://www.yoursite.com/folder-b/newnameforfile2.php [L,R=301]
RewriteRule ^folder-a/(.*)$ http://www.yoursite.com/folder-b/$1 [L,R=301]
</IfModule>
You probably need to put both rules in place: one for the empty folder and one for other files, and then the catch-all at the bottom. This is not tested:
RewriteEngine On
RewriteRule ^folder-a/info.php http://www.yoursite.com/folder-b/new-info.php [L,R=301]
RewriteRule ^folder-a/ http://www.yoursite.com/folder-b/ [L,R=301]
RewriteRule ^folder-a/(.*)$ http://www.yoursite.com/folder-b/$1 [L,R=301]

htaccess redirection from a url to another url

I want to redirect the following url:
http://abc.com/colleges/first.php
to
http://abc.com/first.php
using mod_rewrite and .htaccess
How do I do this?
Try this code in your .htaccess:
Options -MultiViews +FollowSymLinks
RewriteEngine On
RewriteRule ^[^/]+/(.*)$ /$1 [L]
use 301 redirection
example : redirect 301 /old/old.htm http://www.you.com/new.htm
If it's just the first.php page then:
RewriteEngine on
RewriteBase /
RewriteRule ^colleges/first.php$ /first.php
If it's every page in the /colleges folder that needs to be redirected to the root:
RewriteEngine on
RewriteBase /
RewriteRule ^colleges/(.*)$ /$1

Resources