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
Related
I am trying to structure a rewrite rule to implement redirection based on languages.
My directory structure is as follows:
.
├── .htaccess
├── assets
│ ├── css
│ │ └── master.min.css
│ ├── fonts
│ │ ├── fontawesome-webfont.eot
│ │ ├── fontawesome-webfont.svg
│ │ ├── fontawesome-webfont.ttf
│ │ ├── fontawesome-webfont.woff
│ │ ├── fontawesome-webfont.woff2
│ │ └── FontAwesome.otf
│ ├── img
│ │ ├── logo.svg
│ │ ├── slide1.jpg
│ │ ├── slide2.jpg
│ │ ├── slide3.jpg
│ │ └── slide4.jpg
│ └── js
│ └── scripts.min.css
├── de
│ ├── index.php
│ ├── sie.php
│ ├── uns.php
│ └── zusammen.php
├── en
│ ├── index.php
│ ├── together.php
│ ├── us.php
│ └── you.php
├── fr
│ ├── ensemble.php
│ ├── index.php
│ ├── nous.php
│ └── vous.php
└── it
├── index.php
├── insieme.php
├── noi.php
└── voi.php
I have no root index.html or index.php file. I want to have the .htaccess redirect the user to one of the index files inside the language directories by sniffing the browser language and then redirecting the user to the appropriate language. The default language, when the browser's language cannot be sniffed should be French.
My current .htaccess file consists of the following:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteRule ^(en|de|fr|it)/ - [L,NC]
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^(.*)$ /fr/$1 [L]
RewriteRule ^(.*)$ /en/$1 [L]
RewriteRule ^(.*)$ /de/$1 [L]
RewriteRule ^(.*)$ /it/$1 [L]
This seem to function partially, but it isn't able to access the various assets such as images, javascripts, or fonts.
Once the page loads, thereafter, there shouldn't be any problems navigating and changing languages as I have used internal URLs that are directly linking to the specific pages in the appropriate directories.
Any help would be appreciated. Thank you.
You can detect and route using browser's languages. Place this code in root .htaccess
RewriteEngine On
RewriteRule ^(assets|en|de|fr|it)/ - [L,NC]
# detect browser language and capture first 2 chars
RewriteCond %{HTTP:Accept-Language} ^([a-z]{2}) [NC]
# current request is not pointing to a real file
RewriteCond %{REQUEST_FILENAME} !-f
# check if corresponding directory exists
RewriteCond %{DOCUMENT_ROOT}/%1/ -d
RewriteRule ^ %1%{REQUEST_URI} [L]
## default fr rule
# current request is not pointing to a real file
RewriteCond %{REQUEST_FILENAME} !-f
# current request is not pointing to a real directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]+/?$ fr%{REQUEST_URI} [L]
I don't know how you try include assets in you source code, So you can try make this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(img|css|js|fonts)/(.*)$ assets/$2/$3 [QSA,L]
RewriteRule ^(.*)/(fr|it|en|de)/(.*)$ $2/index.php?area=$3 [QSA,L]
RewriteRule ^.htaccess$ - [F]
i have a special situation and i can't find a good solution. I've already seen dozens of questions/answers here but none of them seems to solve my problem!
I have an url like this:
https://subdomain.domain.com/{user-name}/{app-name}/
"user-name" and "app-name" can change everytime and i need to redirect it to
index.php?u={user-name}&a={app-name}
But if the url is only https://subdomain.domain.com/{user-name}/ i need to redirect it to
store-list.php?u={user-name} (to show a list of all available apps for that user).
My .htaccess is currently like this:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /store-list.php?u=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?u=$1&a=$2 [L]
It redirects perfectly on both situations but every call to a "real" file doesn't work (for example a call to a js or css file inside my html).
What am i doing wrong??
Making one RewriteCond Set Apply to Several Rules
Yes, we're really close... but a RewriteCond only applies to one rule. That's what is throwing us off. Let's use some tricky logic and put the conditions in reverse:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L,S=2]
RewriteRule ^([^/]+)/?$ /store-list.php?u=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?u=$1&a=$2 [L]
The conditions say that if the files do exist, leave them unchanged, and to skip the two next rules (S=2).
I never want index.php to show up in my URL, even if the user inputs it. Is this possible?
This is variation whatever after several tries. I've come close a few times but this is where it's at for now.
RewriteEngine On
RewriteBase /sub-dir/
RewriteCond %{REQUEST_URI} index\.php$ //If URL ends in index.php
RewriteRule (.*)index\.php $1 //Somehow remove index.php from the url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Currently I have permalink set up where if the user enters domain.com/sub-dir/my-perma-lin/ it generates a string on the page based on my-perma-link to look like My Perma Link. What I'd like is if the user submits any URL ending in index.php it just removes that from the URL but leaves everything else as is.
domain.com/sub-dir/index.php --> domain.com/sub-dir/
domain.com/sub-dir/my-perma-link/index.php --> domain.com/sub-dir/my-perma-link
I've written quite a few rules in http://htaccess.madewithlove.be/ that work perfectly but when I upload it (to Dreamhost) nothing works.
This for example should work according to the the tester
RewriteEngine On
RewriteBase /sub-dir/
RewriteCond %{REQUEST_URI} \.php //Not needed but thought it would/should help
RewriteRule (.*)(index\.php)+ $1 [L,R=301,NC]
But it just removes everything after /sub-dir/
I'm either missing something super obvious or it's not possible ...
You need to add some flags to your rule:
RewriteEngine On
RewriteBase /sub-dir/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php(\?|\ )
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
You can ditch the RewriteCond %{REQUEST_URI} index\.php$ condition, as that's being checked by the regex in the RewriteRule. You need to include a $ at the end of the regex, and include the flags L to stop rewriting and R=301 to redirect.
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]
I'm new to playing with .htaccess for nicely formatted urls and I'm just not sure I'm doing it right.
My current .htaccess file looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/(.*) search.php?query=$1 [L]
RewriteRule !\.(gif|jpg|ico|css|js|txt|xml|png|swf)$ index.php
What I want is for mysite.com/home/56/page-title to go to index.php where I filter out the number and load the correct index page, which works fine.
Before that, I want to check if the url is pointing at search.php, and if so redirect to mysite.com/search/the-search-term, this also works fine.
What isn't working is if I try to visit a specific .php file say mysite.com/control_panel.php - it just takes me to index.php, but I thought that the following line stopped that from happening?
RewriteCond %{REQUEST_FILENAME} !-f
If someone could explain it to me that would be great :)
Thanks!
1. Order of rules matters
2. RewriteCond directives will only be applied to the ONE RewriteRule that follows it. If you need to apply the same conditions to multiple rules you have to write them multiple times or change the rewrite processing logic (multiple approaches available).
Try this one:
RewriteEngine On
RewriteRule ^search/(.*) search.php?query=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(gif|jpg|ico|css|js|txt|xml|png|swf)$ index.php [L]