.htaccess redirect rule permanent domain change - regex

So, basically I have changed the domain name from Domain1.info to Domain2.com
Another thing that I have changed, is the rule for urls inside website
old url structure: www.Domain1.info/lang/page-name.html
new url structure: www.Domain2.com/lang/page-name
I have added the code:
RewriteEngine On
RewriteRule ^(.*)$ http://www.domain2.com/$1 [R=301,L]`
which successfully redirect me from old to new domain, but I want to get rid of the .html ending.

Basically, just exclude the .html part from the first matched group:
RewriteRule ^(.*)\.html$ http://www.domain2.com/$1 [R=301,L]`

You can tweak your regex to match only part before .html:
RewriteEngine On
# to redirect .html URLs
RewriteRule ^(.+?)\.html$ http://www.domain2.com/$1 [R=301,L,NC]
# to redirect other URLs
RewriteRule ^(.*)$ http://www.domain2.com/$1 [R=301,L]

Related

htaccess - Redirect old url to new url matching regex

I want to redirect old URLs from my site to new URLs for a custom forum (Permanent Redirect 301).
There are more than 10,000 pages in this case.
I can not do a RewriteRule for each existing page. Is there a way to perform a RewriteRule using a regular expression?
For example: I want to redirect an URL like http://www.myforum.com/topic/subject-1234-hello-world to http://www.myforum.com/subject/hello-world.
I tried this:
RewriteEngine on
RewriteRule ^/topic/subject-([0-9]+)-([a-z0-9-]+)$ /subject/$2 [R=301,L]
But it doesn't work...
How should I have done?
Just need to escape the hyphens.
You can try the below rule.
RewriteEngine On
RewriteBase /
RewriteRule ^topic/subject\-([0-9]+)\-([a-zA-Z0-9-]+)$ /subject/$2 [R,L]

Add Virtual Folder to all urls (Apache)

Using Apache and .htaccess how can I do the following:
Redirect all urls to include a fake folder.
For example:
http://example.com/
http://example.com/example.php
http://example.com/folder/example.php
redirect to:
http://example.com/fakefolder/
http://example.com/fakefolder/example.php
http://example.com/fakefolder/folder/example.php
fakefolder does not actually exist in the server directories so I also need to point http://example.com/fakefolder/ to http://example.com/.
So basically if the user goes to http://example.com/ I want the url to show http://example.com/fakefolder/ but at the same time still point to http://example.com.
You can use in your htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} !\s/+fakefolder/ [NC]
RewriteRule ^ /fakefolder%{REQUEST_URI} [R=301,NE,L]
RewriteRule ^fakefolder(/.*)$ $1 [NC,L]

htaccess redirect with special ? character for php string mod_rewrite in apache

I need to redirect URL's with a keyword string used in our AdWords campaign back to my home page using an htaccess rewrite rule. This is what I have already tried...
RewriteRule ^/?keyword=(.*)$ http://{HTTP_HOST}/$1 [R=301]
example:
Looking to make /?keyword=carrots redirect to my home page.
In order to handle redirects involving Query Strings, you will need to use a
RewriteCond %{QUERY_STRING}
in your .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} (.+)
RewriteRule ^$ http://%{HTTP_HOST}/%1/? [NC,L,R=301]

htaccess rewrite URL and remove GET parameters

I am new to URL rewrite, regex and .htaccess
Here is an issue I am facing:
I have this url with GET parameter:
www.mysite.in/alpha-beta/abc.php?id=APPLE%strike=200.00
I want to display it like:
www.mysite.in/alpha/beta/APPLE/200.00
This is the code in .htaccess:
RewriteRule ^alpha/beta/(.*)/([0-9]+(\.[0-9]+))$ alpha-beta/abc.php?id=$1&strike=$2 [NC,L]
But I only get a blank page when I go to URL: www.mysite.in/alpha/beta/APPLE/200.00
When I change the htaccess rule to:
RewriteRule ^alpha/beta/(.*)/([0-9]+(\.[0-9]+))$ http://www.mysite.in/alpha-beta/abc.php?id=$1&strike=$2 [NC,L]
It redirects to correct page but the URL is displayed as http://www.mysite.in/alpha-beta/abc.php?id=APPLE&strike=200.00
What seems to be the problem?
Use these rules in root .htaccess:
RewriteEngine On
RewriteRule ^OI_TOOL/IV/(.*)/([0-9]+(?:\.[0-9]+))/?$ OI_ANALYSIS_TOOL/iv_chart.php?symbol=$1&str‌​ike=$2 [NC,L,QSA]
# fix path for getIV.php by redirecting it to /OI_ANALYSIS_TOOL/
RewriteRule ^OI_TOOL/.+?/(getIV\.php)$ /OI_ANALYSIS_TOOL/$1 [L,NC,R=301]

htaccess subdomain redirect when specific script is called

I need when an specific page is called with a subdomain it redirects to www.
I can give you an example:
nuevoleon.domain.com/empresa/
I need that the correct url for SEO purposes is:
www.domain.com/empresa
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^nuevoleon\.domain\.com$
RewriteRule ^ empresa(/|$) http://www.domain.com%{REQUEST_URI} [L,R=301]