htaccess rewrite rule with a number as part of the subdomain name - .htaccess

I have the following which works well for my test website wiki.rehman.website
RewriteEngine On
# main rewrite rule
RewriteRule ^/?wiki(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]
# Redirect / to Main Page
RewriteRule ^/*$ %{DOCUMENT_ROOT}/w/index.php [L]
But it does not work for my other test subdomain wiki2.rehman.website (notice the "2" in the URL).
RewriteEngine On
# main rewrite rule
RewriteRule ^/?wiki2(/.*)?$ %{DOCUMENT_ROOT}/w/index.php [L]
# Redirect / to Main Page
RewriteRule ^/*$ %{DOCUMENT_ROOT}/w/index.php [L]
Is there something I should do when numbers are used in the URL?

Related

301 redirect of rewritten pages

I'm moving an existing projet from root to language folder /fr, in order to create a different english project located in /en.
The project is simple, it consists on two pages (index.php and liste.php).
The first page was receiving a parameter
/index.php?hash=[page-hash]
and was rewriten in the .htaccess as
/[page-hash].html
So here is the content of the old .htaccess:
RewriteEngine On
######### Page rewriting ########
RewriteRule ^liste\.html$ liste.php [QSA,L]
RewriteRule ^(.*)\.html$ index.php?hash=$1 [QSA,L]
I want the old rewritten pages (www.domain.com/[page-hash].html), which are indexed by Google, to be 301 redirected to www.domain.com/fr/[page-hash].html so that Google still find them at new new location and is informed it has been moved.
It's not a rewriting, it's a redirect, and I don't know how to do that. I guess I have to use RewriteCond and [R=301] but I don't know how.
Of course the new pages must also be rewritten (/fr/index.php?hash=[page-hash] into /fr/[page-hash].html), and the root generally redirected to /fr. So I already made this new .htaccess:
RewriteEngine On
######## Redirect from root to /fr #######
RewriteRule ^$ /fr
Redirect 301 /liste.html /fr/liste.html
Redirect 301 /liste.php /fr/liste.html
######### Page rewriting ########
RewriteRule ^fr/liste\.html$ /fr/liste.php [QSA,L]
RewriteRule ^fr/(.*)\.html$ /fr/index.php?hash=$1 [QSA,L]
RewriteRule ^en/list\.html$ /en/list.php [QSA,L]
RewriteRule ^en/(.*)\.html$ /en/index.php?hash=$1 [QSA,L]
You're pretty close. You can condense those and you only need QSA (and B) when your substitution contains a query.
RewriteEngine On
######## Redirect from root to /fr #######
RewriteRule ^$ /fr/ [L,R=301]
RewriteRule ^([^/]+)\.(?:html|php)$ /fr/$1.html [L,R=301]
######### Page rewriting ########
RewriteRule ^(fr/liste|en/list)\.html$ $1.php [L]
RewriteRule ^(fr|en)/([^/]+)\.html$ $1/index.php?hash=$2 [L,B,QSA]
Here is the complete answer. The order is important.
RewriteEngine On
######### Page rewriting ########
RewriteRule ^fr/liste\.html$ /fr/liste.php [L]
RewriteRule ^fr/(.*)\.html$ /fr/index.php?hash=$1 [L,B,QSA]
RewriteRule ^en/list\.html$ /en/list.php [L]
RewriteRule ^en/(.*)\.html$ /en/index.php?hash=$1 [L,B,QSA]
################ Redirect from root to /fr ################
RewriteRule ^$ /fr [L,R=301]
RewriteRule ^liste\.html$ /fr/liste.html [L,R=301]
RewriteRule ^(.*)\.html$ /fr/$1.html [L,R=301]
This can still be condensed (see Walf's answer).

.htaccess rules - How to redirect incoming "/login.php" url request to "index.php?do=login"?

I want to redirect all incoming url requests to index.php via .htaccess
Example:
/johngroup -> /index.php?do=johngroup
/addmem -> /index.php?do=addmem
/autoshare -> /index.php?do=autoshare
/spamwall -> /index.php?do=spamwall
index.php will handle all requests.
Following code is not working:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([A-Za-z0-9_-.]*).php? do=(.*)
RewriteRule .* /%1.php?do=%2 [R=301,L]
</IfModule>
This works for me:
RewriteEngine On
RewriteRule ^index.php - [L] # 1
RewriteRule ^(.*).php$ index.php?do=$1 [L] # 2
RewriteRule ^(\w+)$ index.php?do=$1 # 3
Explanation:
If urls starts with /index.php, stop processing. Otherwise proceed to next rule.
If url format is /x.php, rewrite to /index.php?do=x and stop processing further. Otherwise proceed to next rule.
Convert any url e.g /test, to /index.php?do=test.
You can also test and tweak htaccess rules using these sites:
Htaccess Tester
Rewrite Rule Tester
example.com/test.php to example.com/index.php?do=test
Try the following in your root .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^index\.php$
RewriteRule ^(\w+)\.php$ index.php?do=$1 [L]
The above will internally rewrite /test.php to /index.php?do=test (ie. /test.php remains in the browsers address bar), regardless of whether test.php exists as a physical file on the filesystem. Basically, any file that has a .php file extension. The RewriteCond directive prevents a rewrite loop.
If you only need to rewrite URLs that would map to physical files, eg. test.php is a physical file on the filesystem then add an additional condition to check for the existence of this file before rewriting:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^index\.php$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(\w+)\.php$ index.php?do=$1 [L]

Mod_rewrite - redirect old urls to the new ones

I'm looking for a solution to redirect old urls (e. g. aboutme.php) to the new ones (same /about-me).
The problem is: if I'll go to example.com/aboutme.php, the user is not redirected to pretty url (/about-me). Adding R=301 doesn't help - it makes /about-me redirect to aboutme.php.
Here's my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*) http://example.com/$1 [R=301,L]
# Specify search friendly URLs
RewriteRule ^about-me$ /aboutme.php [L]
RewriteRule ^portfolio$ /portfolio.php [L]
RewriteRule ^motion$ /motion.php [L]
RewriteRule ^contact$ /contact.php [L]
RewriteRule ^contact-thanks$ /contact_thanks.php [L]
</IfModule>
You have to make it in the other direction.
If you want to rewrite aboutme.php to about-me
you should use
RewriteRule ^aboutme.php /about-me [R=301,L]
EDIT:
The place /about-me has to be present. If not, you can redirect this virtual place to a file (say rewrite.php) which gets the actual file from database or some configuration. Place the following rules at the bottom of your htaccess rules.
#don't rewrite the rewrite script
RewriteRule rewrite.php - [L]
#as example don't rewrite the image folder
RewriteRule ^images - [L]
#everything else that doesn't match a rule
RewriteRule ^(.*)$ rewrite.php?file=$1 [QSA,L]
after that you can access your requested virtual file in rewrite.php with $_GET['file'].
EDIT:
your rewrite.php could look like this: (this is the simplest way todo. but you have to do some error checking etc.)
<?php
$file = $_GET['file'];
$config = array(
'about-me' => 'aboutme.php'
);
include($config[$file]);
?>

Endless Redirect Loop by htaccess rules multi language

i'm not good in writing modrewrite access rules and would like to achieve the following scenario:
virtual url /en/shop.php redirects to /shop.php?lang=en
already got that but the actual problems are within the modrewrite environment:
if url is '/' (empty) --> redirect to /en/ (/index.php?lang=en)
if url is '/en' or '/de' --> redirect to /en/ or /de/ (add slash)
if an uri is "defined" like /en/shop.php -> redirect to /shop.php?lang=en
i've tried several rules but the last one results in a endless loop and i can't figure out what's wrong .. :/ please help
here's my .htaccess file:
RewriteEngine On
RewriteBase /
# empty url -> redirect to en/
RewriteRule ^$ en/ [R=301,L]
# url is ONLY '/en' or '/de' -> redirect to /en/ or /de/ (adding slash)
RewriteRule ^(en|de)$ $1/ [R=301,L]
# now all urls have en/ de/ -> parse them
RewriteRule ^(en|de)/(.*)$ $2?lang=$1&%{query_STRING} [R=301,L]
This is the line that is causing the redirect loop:
RewriteRule ^(en|de)/(.*)$ $2?lang=$1&%{query_STRING} [R=301,L]
Because it is redirecting the browser to /?lang=en (for example). Then the first rule:
RewriteRule ^$ en/ [R=301,L]
Redirects it to /en/?lang=en, then the last rule redirects it to /?lang=en, then the first rule, etc.
You probably meant the last rule to be internal, additionally, your first rule needs to check if there's already a query string parameter, "lang":
RewriteEngine On
RewriteBase /
# empty url -> redirect to en/
RewriteCond %{QUERY_STRING} !lang=(en|de)
RewriteRule ^$ en/ [R=301,L]
# url is ONLY '/en' or '/de' -> redirect to /en/ or /de/ (adding slash)
RewriteRule ^(en|de)$ $1/ [R=301,L]
# now all urls have en/ de/ -> parse them
RewriteRule ^(en|de)/(.*)$ $2?lang=$1&%{query_STRING} [L]
# no "R=301" here --------------------------------------^

redirecting to multiple virtual subdomains using htaccess

I am trying to create subdomains via htaccess. The code below does exactly want I want
It takes http://domain.com and redirect it to http://www.domain.com
Options -Indexes
DirectoryIndex index.html index.htm index.asp index.php
ErrorDocument 401 http://www.domain.com
ErrorDocument 403 http://www.domain.com
ErrorDocument 404 http://www.domain.com
ErrorDocument 500 http://www.domain.com
ErrorDocument 507 http://www.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^(.*) http://www.domain.com/$1 [QSA,L,R=301]
AddType text/html .html .htm .asp
This is the part I am not to sure of:
RewriteCond %{HTTP_HOST} ^domain.com/nl$
RewriteRule ^(.*) http://nl.domain.com/$1 [QSA,L,R=301]
How can I create virtual subdomains so that if someone goes to http://nl.domain.com it would stay on http://nl.domain.com if someone types http://www.nl.domain.com it would take out the http://www.nl.domain.com and make it http://nl.domain.com also the directory structure for the subdomain would be http://www.domain.com/nl (This is where the actual files will be sitting).
so if someone goes to http://www.domain.com/nl it should also redirect to http://nl.domain.com.
Thanks in advance for any advice and pointers
RewriteEngine on
# The ordering of the following rules is somewhat important
#
# External redirects with HTTP "301 - Moved Permanently" for subdomains
#
# Redirect www.nl.example.com to nl.example.com
RewriteCond %{HTTP_HOST} ^www\.nl\.example\.com$
RewriteRule ^(.*) http://nl.example.com/$1 [QSA,L,R=301]
# Instead I could do this to redirect any prefix before nl to nl.example.com
# RewriteCond %{HTTP_HOST} ^.+?\.nl\.example\.com$
# RewriteRule ^(.*) http://nl.example.com/$1 [QSA,L,R=301]
# Redirect www.foo.example.com to foo.example.com
RewriteCond %{HTTP_HOST} ^www\.foo\.example\.com$
RewriteRule ^(.*) http://foo.example.com/$1 [QSA,L,R=301]
# Instead I could do this to redirect any prefix before foo to foo.example.com
# RewriteCond %{HTTP_HOST} ^.+?\.foo\.example\.com$
# RewriteRule ^(.*) http://foo.example.com/$1 [QSA,L,R=301]
# Rewrite any remaining subdomains to example.com
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteCond %{HTTP_HOST} !^nl\.example\.com$
RewriteCond %{HTTP_HOST} !^foo\.example\.com$
RewriteRule ^(.*) http://example.com/$1 [QSA,L,R=301]
# Assuming from this point forward we have either
# example.com, nl.example.com, or foo.example.com as the HTTP_HOST
# Redirect example.com/nl to nl.example.com
# (Note that ONLY example.com/nl is caught here.)
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^nl(/(.*))? http://nl.example.com/$2 [QSA,L,R=301]
# Redirect example.com/foo to foo.example.com
# (Note that ONLY example.com/foo is caught here.)
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^foo(/(.*))? http://foo.example.com/$2 [QSA,L,R=301]
#
# Internal rewrites for directory structuring
#
# Internal rewrite for the nl subdomain
# - Match the subdomain exactly
RewriteCond %{HTTP_HOST} ^nl\.example\.com$
# - Check to see if the rewrite already happened (prevent
# infinite loop of internal rewrites)
RewriteCond %{REQUEST_URI} !^/nl(/.*|$)
# - Rewrite the URL to the subdirectory
RewriteRule ^(.*) /nl/$1 [L]
# Internal rewrite for the foo subdomain
# - Match the subdomain exactly
RewriteCond %{HTTP_HOST} ^foo\.example\.com$
# - Check to see if the rewrite already happened (prevent
# infinite loop of internal rewrites)
RewriteCond %{REQUEST_URI} !^/foo(/.*|$)
# - Rewrite the URL to the subdirectory
RewriteRule ^(.*) /foo/$1 [L]
I haven't tested the above on a server, but I tested it on my local server, it should be close to what you need if I understood you correctly.
I'm sure you've seen the mod_rewrite docs. In addition to that, the Rewrite Guide and the Advanced Rewrite Guide have helpful practical examples.

Resources