Basic redirect using mod_rewrite - mod-rewrite

I want to have a redirect using mod_rewrite.
When someone opens http://aswt.mobileworldindia.com
then they will be automatically redirected to http://www.mobileworldindia.com/panels/mobile/index.php?subdomain=aswt
Please note that http://aswt.mobileworldindia.com is not an actual subdomain and doesn't exist at all and neither I want to have it. I just want to have a redirect in my site.
Tried following code but didnt work, can anybody tell me where I am wrong.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.+)\.sellingsellingsold\.co.uk$
RewriteRule ^$ /index.php?subdomain=%1 [L,R=301]
This is uploaded at sellingsellingsold's root and I am trying to open http://aswt.sellingsellingsold.co.uk.
Please help me in getting it sorted. It has been 2 weeks since I am trying to achieve the redirection.

Something like this inside the main server configuration or inside a .htaccess file. Using the server configuration is preferred and more reliable.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(aswt)\.mobileworldindia\.com$
RewriteRule ^$ /panels/mobile/index.php?subdomain=%1 [L,R=301]
If you want to redirect from any possible subdomain instead of just "aswt." use (.+) instead of (aswt).

Related

Magento, redirect the url from "www.domain.com/home" to base url "www.domain.com"

I am trying to configure how to redirect my magento website from www.domain.com/home to base url www.domain.com, it always redirecting to www.domain.com/home
UPDATE
Thanks Amit, I use your solution
RewriteEngine on
RewriteCond %{THE_REQUEST} ^.*/accueil
RewriteRule ^(.*)accueil$ http://www.cabas-durables.fr/$1 [R=301,L]
but I got error at the browser says
The page isn't redirecting properly
then I change the $1 after http://www.cabas-durables.fr/$1 to index.php, then it shows "http://www.cabas-durables.fr/index.php" at the browser, then I bring it back to $1 I got no error but it stays to "http://www.cabas-durables.fr/index.php"
I don't know where to change it to only "http://www.cabas-durables.fr/"
I will have problem with SEO if it not change
thanks
It can be better to use htaccess.
find
Options +FollowSymLinks
RewriteEngine on
Then
Put the below code in htaccess file
RewriteCond %{THE_REQUEST} ^.*/home
RewriteRule ^(.*)home$ http://www.domain.com/$1 [R=301,L]
Create URL Redirect in admin panel.
Use tagret path - "../"; it will redirect to the homepage.

Redirect URL with htaccess

A third party plugin returns to an incorrect URL from a call to save a change.
The URL is /admin/?page=configure/admin/. The correct return should be to /lists/admin/?page=configure. My attempt to write a redirect failed with a 500 server error.
RewriteEngine On
RewriteRule ^(.*)/admin/(.*)$ $1/lists/admin/$2 [NC,L]
How can I correct this code?
This should work.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^lists
RewriteRule ^(.*/)?admin/(.*)$ $1lists/admin/$2 [QSA,L]
If you want to match a different folder to redirect to admin you will have to declare it literally as a pattern like ^(.*)?/admin would also match lists/admin and cause a loop.

Mod_Rewrite for URL

I have been looking through questions and answer for days trying to figure out how to make this work.
So far I can get my URL to change, but it won't load the page.
I have to take
http://www.mysite.com/index.php?mode=about
And have it show up as
http://www.mysite.com/about/
So far I have the following code:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^mode=(.*)
RewriteRule ^ http\:\/\/\www.mysite.com\/%1? [R=301,L]
RewriteRule /(.*) /index.php?mode=%1 [L]
I have changed things multiple times and nothing. Most site seem to tell me I don't need the 301 redirect but then I can't get anything to work.
For (your) example, once you've properly routed from mysite.com/index.php?mode=about to mysite.com/about, it's now going to look at mysite.com/about/ to find what comes next (index.py/index.html/etc).
Because there is nothing at /about/, you're getting a 404 error.
I don't think you can use mod_rewrite to do exactly what you're trying to achieve, without having some handling within /about/ to actually display the page you want once you get there.
http://www.noupe.com/php/10-mod_rewrite-rules-you-should-know.html
Remember the Filesystem Always Takes Precedence
The filesystem on your server will always take precedence over the
rewritten URL. For example, if you have a directory named “services”
and within that directory is a file called “design.html”, you can’t
have the URL redirect to “http://domain.com/services”. What happens is
that Apache goes into the “services” directory and doesn’t see the
rewrite instructions.
To fix this, simply rename your directory (adding an underscore to the
beginning or end is a simple way to do that).
I have to take
http://www.mysite.com/index.php?mode=about
And have it show up as
http://www.mysite.com/about/
There are two very common types of rules that people want and your statement can be interpreted two ways which require different rules. I'm going to interpret your statement that you have a real, operational script at http://www.mysite.com/index.php?mode=about, but instead of having the user enter that "ugly" URL, you want them to be served that URL when they enter http://www.mysite.com/about/. To accomplish this, you would do the following:
RewriteRule ^about/?$ /index.php?mode=about [L]
Because of the potential for misunderstanding, it's best to state what you want as (1) What the user will enter into their browser and (2) what real file you want to serve them.
I don't believe you need lines #2 & 3 & you seem to have % instead of $, try:
RewriteEngine on
RewriteRule ^([^\/]+) /index.php?mode=$1 [L]
Solved the problem. Thanks for all the help.
#< IfModule mod_rewrite.c>
# RewriteEngine on
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
#< /IfModule>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?mode=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?mode=$1
You can use this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^([^/]+)/$ /index.php?mode=$1 [L]
Where the beginning still allows for other folders to be accessible.

How to redirect all *.html pages to *.php pages?

I looked for the answer to this since it should be a rather common thing to do
and found some but the all lead me to an internal server error.. :(
i need to redirect all my html pages to their counter part in php
i know there is a rewriteRule i can use but cant seem to find the syntax..
a simple explenation:
i need to redirect all my *.html pages to *.php
I hope i explained it well.. would love your help or a link to some resources on this subject where i can really understand how to create this redirectRule
Create a .htaccess in the root folder of your site and place the following rules in it:
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)\.html$ $1.php [L,R]
If you don't want the address bar to reflect the change, then change that last line to
RewriteRule ^(.*)\.html$ $1.php [QSA,L]
You can use RewriteRule:
RewriteEngine on
RewriteRule ^/(.*)\.html$ $1.php [R]
If you get Internal Server Errors, make sure that you have enabled mod_rewrite in apache :
sudo a2enmod rewrite
sudo /etc/init.d/apache2 reload

RewriteEngine: subdomain to index.php, how?

Days later I asked about redirecting dynamic directories to index.php, and I got this code that works perfect (it's the only code I have in .htaccess):
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*) index.php
This translates urls like http://mydomain.example/dynamicdir/ to http://mydomain.example/index.php
Now I want to translate subdomains like http://dynamicdir.mydomain.example to http://mydomain.example/index.php?dir=dynamicdir
From examples I found in Internet I tried adding this line:
RewriteRule ^(.*)\.mydomain\.example index.php?dir=$1
But it doesn't work. I don't have enough experience with mod-rewrite to tell what's missing or wrong. Could you please help me to find a way to keep the dynamic directory translation, and add the catch-all subdomain rule?
Regards!
The mod_rewrite rules use the request path, which is relative to the virtual host. Try having different rewrite rules for each virtual host, but placing their documents in the same directory.
With RewriteRule you can only test the URL path. For the host name, you need to use %{HTTP_HOST} in a RewriteCond:
RewriteCond %{HTTP_HOST} ^(.+)\.example\.example$
RewriteRule ^ index.php?dir=%1

Resources