.htaccess redirect certain strings - apache

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.

Related

.htaccess if subdomain show www.site.com/subs/. no redirect url must bu sub.site.com

I have problem with .htaccess.
I need:
if $_SERVER["HTTP_HOST"] is sub.site.com must show content from www.site.com/sub/. But URL must be like sub.site.com.
And if sub.site.com/content/ must show content from www.site.com/sub/content/
Is it Possible?
Setting subdomains from hosting not working for me because of my CMS.
Try adding these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub.site.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/sub%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/sub%{REQUEST_URI} -d
RewriteRule ^(.*)$ /sub/$1 [L]
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 %{HTTP_HOST} ^sub\.site\.com$ [NC]
RewriteRule (?!^sub/)^(.*)$ /sub/$1 [L,NC]

Apache Rewrite doesn't work to turn any domain name into a .com domain from a .co.uk one

I am using the following rule to turn any domain name into a .com domain from a .co.uk one. Why doesn't this work? Do people have a better way of doing this?
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^(.*).co.uk(.*)$ [NC]
RewriteRule $1.com$2 [R=301,L]
</IfModule>
Your rewrite rules's syntax is wrong. This is the code that should work for you:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.+?)\.co\.uk$ [NC]
RewriteRule ^ http://%1.com%{REQUEST_URI} [R=302,L]
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.

Apache Mod_rewrite 301 redirection

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]

mod_rewrite keeps adding .html for files

I have a redirect situation where the site is part dynamic and part generated .html files.
For example, mysite.com/homepage and mysite.com/products/42 are actually static html files
Whereas other URLs are dynamically generated, like mysite.com/cart
Both mysite.com and www.mysite.com are pointing to the same place. However I want to redirect all of the traffic from mysite.com to www.mysite.com.
I'm so close but I'm running into an issue where Apache is adding .html to the end of my URLs for anything where a static .html file exists - which I don't want.
I want to redirect this:
http://mysite.com/products/42
To this:
http://www.mysite.com/products/42
But Apache is making it this, instead (because 42.html is an actual html file):
http://www.mysite.com/products/42.html
I don't want that - I want it to redirect to www.mysite.com/products/42
Here's what I started with:
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [R=301,L]
I tried making the parameters and the .html optional, but the .html is still getting added on the redirect:
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)?(\.html)?$ http://www.mysite.com/$1 [R=301,L]
What am I doing wrong? Really appreciate it :)
Here is the code you will need in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## first add www to your domain for and hide .html extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ http://www.mysite.com%1 [R=301,L]
## add www to your domain for other URIs without .html extension
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^ http://www.mysite.com%{REQUEST_URI} [R=301,L]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f [NC]
RewriteRule ^ %{REQUEST_URI}.html [L]
Maybe you should try looking at apache's mod_negotiation to get rid of the .html or any file extension?
Link: http://httpd.apache.org/docs/2.0/mod/mod_negotiation.html

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]