htaccess multilanguage in codoforum script - .htaccess

I installed a codoforum for my project and now I want to make it multi language
I did all the language switching in code and now I'm left with implementing it to a link.
Here is their htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
Options +Indexes
RewriteEngine on
# if your app is in a subfolder
#RewriteBase /my_app/
# test string is a valid files
RewriteCond %{SCRIPT_FILENAME} !-f
# test string is a valid directory
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?lang=ru&u=/$1 [NC,L,QSA] #My lang variable
# with QSA flag (query string append),
# forces the rewrite engine to append a query string part of the
# substitution string to the existing string, instead of replacing it.
</IfModule>
Question:
Can I some how get my language variable in the $u var
RewriteRule ^(.*)$ index.php?&u= /mylangvar/ $1
My link looks like this
category/general-discussions
I want it to look like this
langvar/category/general-discussions
If I disable pretty urls
/index.php?u=/category/general-discussions

I did it myself, sorry for dumb question.
Here is what I changed
RewriteRule ^(lv|en|ru)/(.*)$ index.php?lang=$1&u=/$2 [NC,L,QSA]

Related

dynamic to static url AND removing the dynamic

I want to change alle dynamic url's to static ones,
but after rewriting the dynamic url's are still responding/available.
What did I do =>
I found this Tool for SEO:
http://www.webconfs.com/url-rewriting-tool.php
I entered this:
.../filmdetails.html?var=ich_einfach_unverbesserlich_ii
Then I put into my .htaccess this:
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^filmdetails/(.*)/$ filmdetails.html?var=$1
Works, but now I got a problem: This URL is still available and should not be: .../filmdetails.html?var=ich_einfach_unverbesserlich_ii
How do I get rid of the dynamic url's?
Your rule only rewrites the nicer looking URL to the one with a query string. Rules only work from a "pattern" -> "target" way, the mapping won't magically work the other way. You'll have to create a separate rule in order to redirect the browser:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /filmdetails\.html\?var=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /filmdetails/%2/?%3 [L,R=301]
Try:
Options +FollowSymLinks
RewriteEngine On
#set this to path to your filmdetails.html file (from the document root)
RewriteBase /
#checking if redirect already happened
RewriteCond %{ENV:REDIRECT_PASSED} !^$
RewriteRule $ - [L]
#Your rewrite rule
RewriteRule ^filmdetails/(.*)/$ filmdetails.html?var=$1 [L,E=PASSED:1]
#redirecting from filmdetails.html with query string ?var=something
RewriteCond %{QUERY_STRING} ^var=(.+)$ [NC]
RewriteRule ^filmdetails.html$ filmdetails/%1/? [R]
filmdetails.html?var=something will be redirected to filmdetails/something

multiple slashes on url: how to remove?

Based on code found here: remove multiple trailing slashes mod_rewrite
I have the following htaccess
Options +FollowSymLinks
DirectorySlash Off
RewriteEngine on
RewriteOptions inherit
RewriteBase /
#
# remove multiple slashes from url
#
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s(.*/)/+\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L]
#
# Remove multiple slashes anywhere in URL
#
RewriteCond %{THE_REQUEST} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
Yet i found out the G-Bot has crawled this url: http://www.example.com/aaa/bbb/////////bbb-ccc/bbb-ddd.htm. (aaa, bbb, ccc, ddd, are keywords in url, not to be taken litraly - i jut show the pattern of the url)
Testing the above url in by live server i found out that the slash removal does not work.
Anyone can offer any tips or improvement to the the existing code? Thank you
EDIT 1
#Sylwester provided the following code
# if match set environment variable and start over
RewriteRule ^(.*?)//+(.*)$ $1/$2 [E=REDIR:1,N]
# if done at least one. redirect with 301
RewriteCond %{ENV:REDIR} 1
RewriteRule ^/(.*) /$1 [R=301,L]
It is not working either. I still see the ////// inside the url.I have put this set of rules at the very top of my htaccess file, right below the " RewriteBase /", so as not to be affected by other rules, yet... nothing.
Any other suggestion?
Per directory and .htaccess is tricky since apache actually have removed redundant slashed for us. Eg. there is no match for //+ anymore so we check the %{REQUEST_URI} since it has the original URI while the rewrite rule need to match anything:
# NB: Only works for per directory and .htaccess
# Needs "AllowOverride All" in global config for .htaccess
RewriteEngine On
RewriteBase "/"
Options +FollowSymlinks
# Check if the REQUEST_URI has redundant slashes
# and redirect to self if it has (which apache has cleaned up already)
RewriteCond %{REQUEST_URI} //+
RewriteRule ^(.*) $1 [R=301,L]
If you can add global config I would have prefered this in the virtual host instead:
RewriteEngine On
# if match set environment variable and start over
RewriteRule ^(.*?)//+(.*)$ $1/$2 [E=REDIR:1,N]
# if done at least one. redirect with 301
RewriteCond %{ENV:REDIR} 1
RewriteRule ^/(.*) /$1 [R=301,L]

HTACCESS: rewrite a uri request [properties/a-zto myphp php file

My goal is to achieve the following:
My query: properties.php?id=1234-N-StreetName-etc
to be turned into:
properties/1234-N-Beverly-Blvd.html
properties.php will evaluate the value of $id (In this case any-address.html) against a mysql database. If a value is not in the database, the file properties.php will redirect the user to a 404 page.
Where $id's value can be any combination of alphabetical characters, dash '-', or numbers.
The code I have is not working:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]
RewriteRule ^properties/([a-zA-Z0-9_-]+)\.html$ properties.php?id=$1
Can you please help me get the right htaccess rewrite-code?
PS. I know nothing about .htaccess. I modified this code form a sample website.
You will need L flag to mark that rule as Last and QSA for Query String Append.
Use this code in .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^properties/([^.]+)\.html$ /properties.php?id=$1 [L,NC,QSA]
To apply this rule in a sub directory sun use this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /sub
RewriteRule ^properties/([^.]+)\.html$ /sub/properties.php?id=$1 [L,NC,QSA]
# translate html -> php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)\.html$ /$1.php [L,NC]
This will handle URI such as /sub/properties/abc-123.html and forward it to /sub/properties.php?id=abc-123

Rewriting url in htaccess not quite sure how to do it

Hey guys i'm trying to convert url which looks like this
example.com/sometype/author-name/sort-date
or
example.com/sometype/author-name/sort-date/something-value/
Just need a generic solution.
I'm pretty new to this and a little weak at regular expressions.
So what i could write was this to identify
sometype/author-name/sort-date/
or
sometype/author-name/sort-date/something-value/
/([a-zA-Z0-9]+)/ ([a-zA-Z0-9]+)-([a-zA-Z0-9]+)
I want to rewrite this into this format
example.come?data={"par1":"sometype","author":"name","sort":"date","something":"vale"}
or
example.come?data={"par1":"sometype","author":"name","sort":"date"}
First understand what you're asking is very unusual and extremely tricky for mod_rewrite. Not only it required recursion based code but also this code won't be very readable.
With that cautionary note enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)(.*?)/?$ $1\",\"$2$3 [L]
RewriteRule ^([^-]+)-([^,]+)(.*)$ $1\":\"$2$3 [L]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ /?data={\"part1\":\"$1\"} [L]

How to redirect in .htaccess? => /ua/ to /?lang=ua

I have a multilingual website and .htaccess, which displays the all page and language ?lang=ua style.
I want to redirect (using code 301) asks site.com/en/ to site.com/?lang=en with RewriteEngine.
Example:
site.com/en/ => site.com/?lang=en
site.com/ua/news.html => site.com/news.html?lang=ua
site.com/ua/news/2-material-two.html => site.com/news/2-material-two.html?lang=ua
and so on much...
How to prepare Htaccess file for Apache to meet this criterion? And how do?
Thanks in advance
Put this code in your .htaccess file under DOCUMENT_ROOT dir:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]+)/(.*)$ /$2?lang=$1 [L,R=301,QSA]
Try this:
Options +FollowSymlinks
RewriteEngine on
RewriteOptions MaxRedirects=10
RewriteBase /
RewriteRule http://site.com/([^/]+)/(.+).html$ http://site.com/$2.html?lang=$1 [R=301,NC]
I've just put this together off the top of my head so it may not be quite right, but the idea is that it will examine the url and the ([^/]+) should read the 1st directory, and then read everything up to the .html.
It then rewrites the url to be site.com/[everything after 1st dir up to .html].html?lang=[name of 1st dir]
Add the following to your htaccess file in the root of your domain
RewriteEngine On
RewriteBase /
#if the lang param is not present
RewriteCond %{QUERY_STRING} !^lang=
#capture the language code and redirec to url with lang param
RewriteRule ^([a-zA-Z]+)/(.+\.html)$ /$2?lang=$1 [L,R=301,NC]
Please, if you must use general "possibly directory matching"-rule, consider the !-d operator (and optional: the !-f operator).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/ http://site.com/?lang=$1 [R=301,L]
So you won't end up fixing all kinds of strange problems with existing files and/or directories.

Resources