Redirect using .htaccess - apache

I want to redirect all user page requests to a page on the same domain.
For example, I have an "under construction, BRB" page that I want all users to see when they try to access ANY page on the site.
I tried using this:
Redirect 302 / http://www.domain.com/index2.php
What that does is try to apply the redirect to the index2.php page as well and it gets stuck in a loop where the user then sees this until the browser stops.
http://www.domain.com/index2.phpindex2.phpindex2.phpindex2.php etc., etc,
Any idea on how to write that rule to except that page?

You have to exclude the file you want to redirect to. Here’s an example with mod_rewrite:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule !^index2\.php$ /index2.php [L,R=302]
</IfModule>

To prevent endless looping (index2.php <--> index2.php):
Only redirect If URL to be redirected does NOT contain the string 'index2.php'
RewriteCond %{QUERY_STRING} !index2.php
RewriteRule ^(.*)$ /index2.php [L,R=301]

You could use mod_rewrite
<IfModule mod_rewrite.c>
RewriteCond {REQUEST_URI} !=/index2.php
RewriteEngine on
RewriteRule ^.*$ /index2.php
# End .HTACCESS
</IfModule>

I'd be more inclined to use the slightly different
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/.*$ /index2.php [R=307,L]
</IfModule>
This will let you return a moved temporarily status for the redirect.
Omitting this set of flags means that mod_rewrite will return a 302 Found status by default.
HTH
cheers,

Related

Htaccess rewrite rule redirected but server not processh

I want my server to read this page example.com/myapp.php?p=11 against the URL example.com/myapp/my-technical-effort.
I have tried this .htaccess rule:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/myapp\.php$
RewriteCond %{QUERY_STRING} ^p=11
RewriteRule ^(.*)$ example.com/myapp/my-technical-effort [QSD,R=301,L]
it redirects to the new URL but the server says "page not found" on this server with 404 error.
Can you please help me to rewrite the rule so the server can serve the page example.com/myapp.php?p=11 against the URL example.com/myapp/my-technical-effort.
My current .htaccess is as follows:
<IfModule mod_headers.c>
Header always append X-Frame-Options ALLOWALL
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"
</IfModule>
Anyone can help me please?
Your implementation is the wrong way round. Have a try that way:
RewriteEngine on
RewriteRule ^/?myapp/my-technical-effort$ /myapp.php?p=11 [QSD,L]
You need to implement your rewrite rules based on actual imcoming requests. Those should get rewritten internally, so those have to be matched.
This asumes that the actual request you want to rewrite is to /myapp/my-technical-effort (your question is a bit vague in that), and that /myapp.php?p=11 is the internal resource you want to get queried.
UPDATE:
Your comment below suggests that you are actually looking for a solution to redirect the "old" URL to the new one. It is indeed possible to combine that with above rewriting:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^p=11$
RewriteRule ^/myapp\.php$ /myapp/my-technical-effort [QSD,R=301,L]
RewriteRule ^/?myapp/my-technical-effort$ /myapp.php?p=11 [QSD,L]
This will redirect any client that requests the "old" url (https://example.com/myapp.php?p=11) to the "new" url (https://example.com//myapp/my-technical-effort). And it will internally rewrite incoming requests to the "new" url to the actual script able to respond to the requests (/myapp.php).

Why is mod_rewrite adding var/www/html to the resulting url

I am trying to make my service backward compatible, since I have moved the service to a new path internaly, I still want the users to access it with the old url as not all of them have knowledge of this change.
I can accomplish what I want if I add [R] flag at the end of my rewrite rule but it redirects the url on the client side, which I don't want.
My rewrite code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/old-path(.*)$ /new-path/$ [L]
</IfModule>
Although this rule results in the following url:
/var/www/html/new-path
Sample request looks something like:
https://host-name/old-path/param1/param2/param3/param4
and rewrite rule should just replace old-path with the new-path.
Can anyone give me some clues about what am I doing wrong? and how can I fix it?
Thanks in advance!
If your are aiming for 'hiding' the rewrite, try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/old-path/(.*)$ /new-path/$1 [P,L]
</IfModule>
And if you REALLY want to extract the hostname:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*)
RewriteRule ^/old-path/(.*)$ http://%1/new-path/$1 [P,L]
</IfModule>
Since %1 references the first bracket of RewriteCond-line...
If you want customers to be rewritten to the new URI and see that:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/old-path/(.*)$ https://host-name/new-path/$1 [R=301,L]
</IfModule>
or in short, and without even using a rewrite:
RedirectMatch ^/old-path/(.*)$ /new-path/$1
To avoid 'https://host-name/' change your DocumentRoot to the parent-directory of 'old-path' and especially 'new-path'.

redirect certain pages to its equivalent in another domain, otherwise redirect to its homepage

I want to use htaccess to redirect certain pages to its equivalent in another domain, other pages redirect to homepage.
Like that:
http://old-domain.com/certain-page redirected to: http://new-domain.com/certain-page
http://old-domain.com/another-certain-page redirected to: http://new-domain.com/another-certain-page
Only these pages will be redirected, otherwise, pages have to be redirected to the new domain home page.
http://old-domain.com/non-certain-page redirected to: http://new-domain.com
This is my try:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^certain-page$ http://new-domain.com/certain-page [R=301,L]
RewriteRule ^another-certain-page$ http://new-domain.com/another-certain-page [R=301,L]
</IfModule>
But don't know how to exclude other pages.
Any help here?
Thanks in advance!
I'm not sure what you mean by
But don't know how to exclude other pages
But you are already excluding the pages when you create specific rules for them. This should work.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#redirect specific pages
RewriteRule ^certain-page$ http://new-domain.com/certain-page [R=301,L]
RewriteRule ^another-certain-page$ http://new-domain.com/another-certain-page [R=301,L]
#Redirect everything else to homepage
RewriteRule ^(.*)$ http://new-domain.com/ [R=301,L]
</IfModule>
Let me know how this works for you. If you need to clear you cache before trying these new rules.
Another answer was mentioned here, thank to anubhava
With redirect without need to use Rewrite :
RedirectMatch 301 ^/.+ http://www.newdomain.com/
So, the answer can be also:
#Redirect specific pages
redirect 301 /certain-page http://new-domain.com/certain-page
redirect 301 /another-certain-page http://new-domain.com/another-certain-page
#Redirect everything else to homepage
RedirectMatch 301 ^/.+ http://new-domain.com

htaccess simple redirect without changing URL

I'm trying to redirect people who go to my site but I want it to not show in the URL.
http://www.example.com/ -> http://www.example.com/dev/
except the /dev part shows in the URL when the redirection takes place, how do I hide it so it stays as http://www.example.com/? Here's my HTAccess file..
RewriteBase /
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ dev/index.php [L]
</IfModule>
Any help would be appreciated, thanks.

Access webpage from subdirectory as root page

I have two websites:
http://example.com
http://example.com/sub
I have a page, let's say:
http://example.com/sub/page1
Is there any way (using .htacces) to make it possible to access page1 via this url:
http://example.com/page1?
Redirect 301 won't do the work, because I don't need redirects. Is there any way to omit '/sub/' for certain urls, like http://example.com/sub/page1 ?
Yep, there is. Do the following within your .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# This would rewrite any URL that starts with /sub to /
RewriteCond %{REQUEST_URI} /sub
RewriteRule ^sub/(.*)$ /$1 [L]
<IfModule>
Edited to meet the OP's requirements.