Apache Mod_rewrite 301 redirection - apache

I am new to Apache. I am trying to make a permanent 301 redirect to the following URL via apache mod_rewrite:
http://www.mysite.com/products.php?page=TheForm
to
http://www.mysite.com/the-form/
The problem is that we have a query string in the first URL. How can I deal with this situation as normally I used the following code but the query string cannot be passed to the RewriteRule.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]
RewriteRule ^products.php?page=TheForm$ http://www.mysite.com/the-form [R=301,L]
</IfModule>

The URI-path tested in the rewrite rule does not contain the query. The QUERY_STRING variable should be used.
You may try this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} page=TheForm [NC]
RewriteRule ^products\.php /the-form/? [R=301,NC,L]

Related

.htaccess redirect everything to new domain except one URL to something different

I am 301 redirecting everything from http://something.org/* to https://something.com/* with this code...
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) https://www.something.com/$1 [R=301,L]
</IfModule>
I am trying to add exception that this URL http://something.org/registration/service redirects to http://www.something.com/registration/service (not HTTPS like general rule). Tried this to exclude this url from general rule and it works, but I don't know how to add rule to redirect this exclusion to something else:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/registration/service
RewriteRule (.*) https://www.dewesoft.com/$1 [R=301,L]
</IfModule>
Replace your RewriteCond with this :
RewriteRule ^registration/service http://example.com%{REQUEST_URI} [NE,L,R]

htaccess redirect for load a specific image url at subdomain from main domain

I have a subdomain (us.example.com) and main domain (example.com). I want to redirect my subdomain url to main domain only for a specific image. For example if I hit http://us.example.com/images/test.jpg then it should redirect to http://example.com/images/test.jpg
I am trying 301 redirect as below but it's not working.
Redirect 301 https://us.example.com/images/test.jpg https://example.com/images/test.jpg
I have tried other rules also but they all work on complete image directory not for a specific image under directory.
Update
Changed rule like this -
RewriteCond %{HTTP_HOST} ^us\.example\.com$ [NC]
RewriteRule ^/?images/test.jpg$ https://example.com/images/test.jpg [R=301,L]
But no luck.
The Redirect directive from mod_alias has the following syntax:
Redirect [status] [URL-path] URL
and, as you see, it receives/accepts the URL path, and not the whole URI/URL for matching.
For the subdomain match, use an <if> directive:
<If "%{HTTP_HOST} == 'us.example.com'">
RedirectPermanent /imges/test.jpg https://example.com/images/test.jpg
</If>
The same can be achieved using mod_rewrite as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^us\.example\.com$ [NC]
RewriteRule ^/?images/test.jpg$ https://example.com/images/test.jpg [R=301,L]
Try this mate ,
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^us.domain.com$ [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
</IfModule>
What will happen for the above .htaccess was ,
If you visit
us.domain.com/somelink
It will redirect to
domain.com/somelink
EDIT :
Suppose if the above doesn't help try the one below with symlinks
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^us.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
Update 1 :
For only test.jpg
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^us.domain.com/images/test.jpg [NC]
RewriteRule (.*) http://www.domain.com/images/test.jpg [R=301,L]
</IfModule>

Stop Apache mod_rewrite affecting Redirect rules

I have the following .htaccess file:
Redirect 301 /test/example-1 /test/example-2
RewriteEngine On
RewriteBase /test/
RewriteRule ^(.*)$ index.php?_url=$1 [QSA,L]
I want the redirect to happen without mod_rewrite interfering with it, but at the moment when you hit /test/example-1 it redirects to /test/example-2?_url=example-1.
Is there any way to stop the query parameter from the rewrite rule getting append to the redirect URL? Thanks.
Have your rewrite rule as:
RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_URI} !/(example-1|index\.php) [NC]
RewriteRule ^(.*)$ index.php?_url=$1 [QSA,L]
But in general you shouldn't mix mod_alias rules with mod_rewrite rules. Your code can be rewritten as:
RewriteEngine On
RewriteBase /test/
RewriteCond %{THE_REQUEST} /example-1[\s?/] [NC]
RewriteRule ^ example-2 [R=301,L]
RewriteRule ^(.*)$ index.php?_url=$1 [QSA,L]

.htaccess redirect certain strings

I have the following setup
go.me.com/123
I would like to redirect go.me.com/anything to me.com but have certain numbers redirect to certain pages.
Is that possible?
Sample Solution
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 /
# first specific rules for specific URIs
RewriteCond %{HTTP_HOST} ^go\.me\.com$ [NC]
RewriteRule ^10$ /sample1 [L,R=302]
RewriteCond %{HTTP_HOST} ^go\.me\.com$ [NC]
RewriteRule ^20$ /sample2 [L,R=302]
# now anything rule
RewriteCond %{HTTP_HOST} ^go\.me\.com$ [NC]
RewriteRule . / [L,R=302]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

Rewriting URL for subdomain redirection

I need to pass through a list of pages to show them coming from subdomain
When a user enters this url in the browser
xyz.mydomain.com/
it should be passed though as
www.mydomain.com/level1/pageA?subdomain=xyz
and
xyz.mydomain.com/innerpage_abc?param1=123
as
www.mydomain.com/level1/innerpage?dynamicparam=abc&param1=123&subdomain=xyz
Enable mod_rewrite and mod_proxy. Then put this code in .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(xyz)\.mydomain\.com$ [NC]
RewriteRule ^$ http://www.mydomain.com/level1/pageA?subdomain=%1 [L,P,QSA]
RewriteCond %{HTTP_HOST} ^(xyz)\.mydomain\.com$ [NC]
RewriteCond %{QUERY_STRING} (?:^|&)param1=([^&]+) [NC]
RewriteRule ^([^_]+)_([^/]+)/?$ http://www.mydomain.com/level1/$1?dynamicparam=$2&subdomain=%1 [NC,L,P,QSA]