URL Rewriting and htaccess files - regex

Hoping someone can help. I use nibbler to check out my website to make sure it behaves well and one of the categories is URL FORMAT which
Avoid use of file extensions wherever possible.... Consider URL rewriting as an effective
and transparent means of creating appropriate URLs.
Anyway, I use Dreamweaver to create and edit the site and if I convert it to using index.htm files in directories (which would solve this) then this just seems to make life complicated.
Is there some MAGIC that I can do in the .htaccess file that means i can still upload it as .htm's and it does some cool stuff in the background and shows them better.
for example I would like
http://www.beingchildren.org/Children-Charity-Blog.htm
to become
http://www.beingchildren.org/Children-Charity-Blog
Can anyone help?

Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.htm[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.htm -f
RewriteRule ^(.+?)/?$ /$1.htm [L]

Related

htaccess removing .php extension and pretty urls

Im trying to both remove .php extensions. So for example "http://localhost/timetable/login" instead of "http://localhost/timetable/login.php"
But also have
"http://localhost/timetable/38/" instead of
"http://localhost/timetable/index.php?week=38"
Im able to get one or the other working but not both at the same time. Im assuming its because there is a conflict between them but Im not advanced enough to find it.
Here is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^([0-9]+)$ index.php?week=$2
RewriteRule ^([0-9]+)/$ index.php?week=$2
If in the address bar I type "http://localhost/timetable/38" it brings me to "http://localhost/38/" and an Object not Found error.
Does anyone know what the problem is ?
UPDATE: I can now go to the page but
echo $_GET['week'];
Is returning empty result, so its ignroing the 40 in "http://localhost/timetable/40"
Instead of using separate rewrite rule for each input, you should consider routing all of them as a single string to some php file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?page=$1 [L,QSA]
In you php file, you can then separate the string as input and use them as required.
<?php
$inputs = explode('/', $_GET['page']);
You only have one capture group when you try to get the week. So it should be $1 instead of $2.
According to this test tool, the following should work:
RewriteRule ([0-9]+)/?$ index.php?week=$1
I would do something like this:
# rewrite if url ends with a number and possibly a slash
RewriteRule ([0-9]+)/?$ index.php?week=$1 [QSA,L]
# do not append .php if it already ends with .php, other add .php
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)$ $1.php [QSA,L]

htaccess to remove .php .html extension if exists from URL

I have pages like the following
http://www.domain.com/p.php?p=InfoPages&file=about.php
http://www.domain.com/p.php?p=InfoPages&file=contact
http://www.domain.com/p.php?p=InfoPages&file=post.html
I want to change them to
http://www.domain.com/page/about
http://www.domain.com/page/contact
http://www.domain.com/page/post
RewriteRule ^page/([^/]*)$ /p.php?p=InfoPages&file=$1
I am currently using the above code in .htaccess but it doesn't work if there is a .php or .html extension in the url.
I've googled for this but still i can't find a perfect way to solve since not all links have extensions.
please advise. thanks.
Try :
RewriteEngine on
RewriteCond %{THE_REQUEST} /p\.php\?p=([^&]+)&file=([^.]+)\.php|html [NC]
RewriteRule ^ /page/%2? [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([^/]+)/?$ /p.php?p=InfoPages&file=$2 [NC,QSA,L]

Combine "folder as GET parameter" and "subdomain as GET parameter"

I have a site that rewrites path after domain to a page GET parameter.
##REWRITING DIRECTORIES TO GET PARAMETERS
RewriteBase /
#Ignore all real directories (do not rewrite them)
RewriteCond %{REQUEST_FILENAME} !-d
#Also do not rewrite real files
RewriteCond %{REQUEST_FILENAME} !-f
#For everything else, index.php should fetch the proper content
RewriteRule ^([^/]*)$ index.php?page=$1 [QSA,L]
##This means:
#example.com/help
#~becomes~
#example.com?page=help
The site uses multiple languages and so far, I've been using cookies to set and remember the language for the user. While the convenience for the user is disputable, this is definitely not convenient for the SEO.
I need to rewrite [a-z]{2}\.mydomain\.xx to index.php?lang=$1 so that user will be always on en.domain.com for example. There are examples to do this, however I'm still confused about how the rewrite engine works and I don't know how should I combine my new rules with the old ones:
##Language rewrite
#Copypasted. Didn't understand
RewriteCond %{HTTP_HOST} ^([a-z]{1,2})\.domain\.xx
RewriteRule ([a-z]{1,2})\.domain\.xx index.php?lang=$1 [QSA,L]
How can I get en.domain.com/help turn in index.php?page=help&lang=en?
How can I get en.domain.com/help turn in index.php?page=help&lang=en
You can use:
RewriteEngine On
RewriteBase /
#Ignore all real directories (do not rewrite them)
RewriteCond %{REQUEST_FILENAME} !-d
#Also do not rewrite real files
RewriteCond %{REQUEST_FILENAME} !-f
#For everything else, index.php should fetch the proper content
RewriteCond %{HTTP_HOST} ^([a-z]{1,2})\.domain\.xx$ [NC]
RewriteRule ^([^/]+)/?$ index.php?lang=%1&page=$2 [QSA,L]
Reference: Apache mod_rewrite Introduction

301 redirect www.root.com/subfolder to www.subfolder.com on same server

Hi people at Stackoverflow,
I've honestly searched here and tried stuff but since I'm really not an expert and nothing worked, I'm lost and need your help. I don't post here often because usually I find what I am looking for.
The situation
I have different sites running on the same server. Let's say that one of my websites is called Cats. It runs with CMSMS and resides in a subfolder of my root called cats.com.
In the root of my server there is a htaccess file with this code (generated by the server admin):
RewriteCond %{HTTP_HOST} ^www.cats.com$
RewriteCond %{REQUEST_URI} !^/cats.com/
RewriteRule (.*) /cats.com/$1
RewriteCond %{HTTP_HOST} ^cats.com$
RewriteCond %{REQUEST_URI} !^/cats.com/
RewriteRule (.*) /cats.com/$1
In the cats.com subfolder there is a htaccess file with rewrite code for pretty urls:
# RewriteBase /cats.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
This works. When I request www.cats.com I see the contents of the /cats.com subfolder in my root but the URL base stays www.cats.com and all the URLS are extremely pretty.
However, due to a setup error (my bad) in my site's config file, the URL listed in Google is
http://www.root.com/cats.com
When I click the URL, I see the correct page but the address bar reads http://www.root.com/cats.com. The pretty URL rewrite works, but I want this url to rewrite to www.cats.com.
What I have tried
I have tried rewriting the Google listed URL with
attempt one
I tried this in both htaccess files, below and above the existing rewrite rules.
RewriteRule ^/cats.com(.*)$ http://www.cats.com [R=301,L]
RewriteRule ^/cats.com/(.*)$ http://www.cats.com/$1 [R=301,L]
Nothing happens. Everything stays the same.
attempt two
Again tried this in both htaccess files, below and above existing rewrite rules
Redirect 301 /cats.com http://cats.com
This results in an infinite loop in all occasions.
I hope my description is clear enough...
If anybody has any idea what might / should work I'd love to hear it.
Thank you for your help!
Change your rules in cats.com subfolder's htaccess file to this:
RewriteEngine On
RewriteBase /cats.com/
RewriteCond %{THE_REQUEST} /+(cats\.com)/(\S+) [NC]
RewriteRule ^ http://%1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]

drupal multisite htaccess: rewrite all languages to default language on one subsite

I have a Drupal 6 multisite, with 2 domains (www.example.com and www.domain.com), sharing some common content.
The domain example.com is in three languages (EN, FR, NL). Languages are set by path prefix (/en, /fr, /nl). The other domain domain.com is just in one language (NL).
The problem: on many occasions domain.com is shown in the wrong language, even if no path prefix is filled in. Somehow it seems to default to EN, though it doesn't always do that - behaviour doesn't seem to be very consistent.
The solution (at least I hope): since I'm not a Drupal developer (I inhereted the site from a former colleague) I have no idea how to fix this in Drupal, so I thought the best way to fix it would be to add some rewrite rules to .htaccess.
I'm no htaccess/regex expert either, and can't get it working. You can find my current rewrite rules below, any help or suggestions are most welcome.
Some examples:
www.domain.com/fr/some-title needs to be rewritten to www.domain.com/nl/some-title
www.domain.com/node/1975 needs to be rewritten to www.domain.com/nl/node/1975
These are the rewrite rules that were already there:
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I tried adding this:
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^/(.*)$ /nl/$1
and would expect this just to prepend /nl/ to all paths (thus not being a full solution since /fr/some-title would become /nl/fr/some-title) - however, a quick test shows me that:
/fr/some-title is rewritten to /nl/some-title (which is what I need, but not what I expected)
/some-title is not rewritten
The question: any ideas what might be wrong? Or could this be caused by other (Drupal) settings? Or is there a better way to solve my problem?
Just for the sake of completeness: the live website is www.cinemazuid.be
If this rule
RewriteRule ^/(.*)$ /nl/$1
is in your .htaccess file, I am surprised that it works as the leading / is always stripped out, so it should theoretically never match any request.
If your desire is to force a default language of NL for those requests that do not specify a language, then add the following rules to the top of your .htaccess file, before any existing rules
#if request is for existing file or directory
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
#then stop processing
RewriteRule .* - [L]
#replace fr with nl. This rule
RewriteRule ^fr/(.*)$ /nl/$1 [L,R=301]
#if the request does not have a language of en or nl
RewriteCond %{REQUEST_URI} !^/(en|nl)/ [NC]
#redirect with nl as default language
RewriteRule .+ /nl%{REQUEST_URI} [L,R=301]
If you do not want to redirect, just drop the R=301
I edited code above to replace /fr/some-title with /nl/some-title/.
The L flag tells mod_rewrite to stop processing further rules, which is usually what you want, unless you have another rule that needs to further process the current request.
#redirect /fr/* and /en/* to /*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(en|fr)/(.*)$ /$2 [R,L]
#internally rewrite /* to /nl/*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond $1 !^nl/$ [NC]
RewriteRule ^(.*)$ /nl/$1
#drupal code
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]