Redirect/Alias of URL in Apache - apache

I have an Apache2.4 server on windows and I have a url like this:
http://example.com/fps/7A/quiz and I wish to redirect it to
http://example.com/fps/7A/quizlet.
I have tried Alias, ScriptAlias and redirect inside the Apache's htaccess file. The alias sometimes caused the whole website and all the pages to break.
Would I have to just make a folder named /quiz/ and put a file that redirects using PHP, or would .htaccess work fine?

Something like this?
RedirectMatch ^/fps/7A/quiz$ /fps/7A/quizlet
Or more general
RedirectMatch ^/fps/([^/]+)/quiz$ /fps/$1/quizlet
Or even more general
RedirectMatch ^/(.*)/quiz$ /$1/quizlet
You can add a 301 after RedirectMatch to make it a permanent redirect:
RedirectMatch 301 ^/(.*)/quiz$ /$1/quizlet

Related

redirect v rewrite for a domain redirect with apache

I want to permanently redirect:
oldsite.com --> newsite.com
oldsite.com/foo --> newsite.com/foo
...etc
I am doing this on a website that uses rewriting (Drupal) on urls.
The IP address for both domains points to the same server. I have vhosts files for both on the same server.
What should I be using when redirecting in the virtualhosts file, the redirect and redirectMatch commands or the rewrite engine?
I want the users to be aware of the redirect.
As an extra issue, I seem to be having trouble with the redirect and redirectMatch commands not properly redirecting sub-pages.
Something like this:
Redirect permanent / https://newsite.com
RedirectMatch permanent ^/(.*)$ https://newsite.com/$1
produces the following:
oldsite.com --> newsite.com (correct)
oldsite.com/foo --> newsite.comfoo (broken)
Should be persevering with redirect or should I be using a Rewrite rule. Why is one better than the other in this case?

Use htaccess in subdirectory to redirect page

I had an html page in a subdirectory - http://example.com/subdir/old.html
I've renamed it to new.html, and I'd like everything linking to old.html to redirect to new.html. However, I'd prefer not to complicate the htaccess in the root. So I made an htaccess file in subdir.
I've tried putting these lines in subdir's htaccess file:
Redirect 302 old.html http://example.com/subdir/new.html
Redirect 302 /old.html http://example.com/subdir/new.html
Redirect 302 http://example.com/subdir/old.html http://example.com/subdir/new.html
But none of them work (http://example.com/subdir/old.html gives a 404). Any tips would be appreciated.
Try RedirectMatch instead: (I couldn't get Rdirect worked in subdir either)
RedirectMatch 302 old.html http://example.com/subdir/new.html

RedirectMatch file not found

So, I have in my web root an admin.php file and I want every time someone writes /admin to be redirected to that /admin.php.
I wrote in my htaccess
RedirectMatch 301 ^/admin$ /admin.php
However, I get an error from apache "file not found".
I created an empty /admin folder and it works...
Then I try to have an other (also non existant folder) to redirect to admin.php. So I write
RedirectMatch 301 ^/blah$ /admin.php
and it works...
Finally I delete the admin.php and I have the thing redirected to another .php
RedirectMatch 301 ^/admin$ /index.php
and it works again...
I understand that for some reason apache messes things up when the folder to be redirected from has the same name with the .php
With Rewrite I can do the redirection without any problems but I was wondering if it was also possible with just a Redirect... Maybe I am missing something here...
apache messes things up when the folder to be redirected from has the same name with the .php
This is due to Apache's content negotiation module that runs before mod_rewrite, mod_alias and makes Apache web server match extensions of files. e.g. /file can be in URL but it will serve /file.php.
To fix this behavior disable MultiViews by placing this rule on top of your .htaccess:
Options -MultiViews

301 Redirect entire virtual host without including URLs

So I have a site setup for http://weddings.gigmasters.com like so:
<VirtualHost *:80>
ServerName weddings.gigmasters.com
Redirect 301 / http://www.gigmasters.com/events/wedding
</VirtualHost>
Now, as you can see, when you go to http://weddings.gigmasters.com, it properly redirects. However, when I go to something like http://weddings.gigmasters.com/a-url-goes-here, it goes to http://www.gigmasters.com/events/weddinga-url-goes-here, which is absolutely not what I want. Is there a way using apache redirect to redirect without the URL appended?
Try Redirectmatch instead with regex capabilities:
RedirectMatch 301 ^/(?!events/wedding$).*$ /events/wedding
So it turns out Apache has RedirectMatch, which can be used like this:
RedirectMatch 301 (.+) http://www.gigmasters.com/events/wedding
...to basically capture any URL and quash it, and then just redirect to the proper URL.

Redirecting all sub-sub pages to another subpage using htaccess

I've a older site running in Apache Server which is already indexed in Google. I wish to redirect all those indexed links to my new site (As the older pages are not existing any more.)
So i wish to redirect all my sub-sub pages to my new root page
I've pages like follows
http://itdost.com/answer-now/Aerobics
http://itdost.com/answer-now/HTML
http://itdost.com/answer-now/Culture
I use the following redirect code for each one
Redirect 301 /answer-now/Engineering http://www.itdost.com/questions/
Redirect 301 /answer-now/Food http://www.itdost.com/questions/
Redirect 301 /answer-now/ASP http://www.itdost.com/questions/
But as the site structure is big, i wish to do it in a single line instead of writing a line for each redirect
Some thing like the following.
Redirect 301 /answer-now/% http://www.itdost.com/questions/
But the above code does not seems to work
In order to use regex better to use mod_rewrite which is more powerful than mod_alias.
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 /
RewriteRule ^answer-now(/.*|)$ http://www.itdost.com/questions/? [L,NC,R=301]
Try this:
RedirectMatch 301 ^/answer-now/ http://www.itdost.com/questions/