Alright im doing a site which is available in multiple languages. I have ran into some problems with my mod rewrite. I'm trying to do this:
Normal page without extra querystrings: /en/whatever
And the page with problems (with querystrings): /en/dashboard/project/projectid
And my .htaccess:
RewriteRule ^en/(.*)$ $1?lang=en [NC,L,QSA]
RewriteRule ^dashboard/project/([a-z0-9]+)$ projectdashboard.php?id=$1
But the ?lang=en is getting lost when opening this page: /en/dashboard/project/projectid
Use the QSA flag in both rules:
RewriteRule ^en/(.*)$ $1?lang=en [NC,QSA]
RewriteRule ^dashboard/project/([a-z0-9]+)$ projectdashboard.php?id=$1 [QSA]
Related
I created a language script in my website. This script recognizes the language in the URL and loads the language definitions.
For example:
RewriteRule ^(.+)/about/?$ sobre2.php?lang=$1 <-- working fine
RewriteRule ^(.+)/contact/?$ contato2.php?lang=$1 <-- working fine
RewriteRule ^(.+)/products/?$ produtos2.php?lang=$1 <-- working fine
RewriteRule ^(.+)/contact/sales/?$ contato-vendas2.php?lang=$1 <-- working fine
RewriteRule ^(.+)/contact/general/?$ contato-form2.php?lang=$1 <-- working fine
The pages above are accessed, respectively, as this:
https://domain.com/b2b/pt-br/about/
https://domain.com/b2b/pt-br/contact/
https://domain.com/b2b/pt-br/products/
https://domain.com/b2b/pt-br/contact/sales/
https://domain.com/b2b/pt-br/contact/general/
This is working fine in all pages, except index.
When I try to do the same thing with the index page, using this code:
RewriteRule ^(.+)/?$ index2.php?lang=$1
Besides it doesn't work, it also affects all the other pages (and resources), so the resources are not loaded and the page looks like a "pure html".
I'd like to know how can I configure the htaccess to make my "index2.php" be accessible like this:
https://domain.com/b2b/pt-br/
Thank you.
.+ will match 1 or more of any character and if you place rule before other rules then this will make other rules ineffective.
You can use these rules:
RewriteEngine On
RewriteBase /b2b/
RewriteRule ^((?!pt-br/).*)$ pt-br/$1 [L,R=301,NC]
RewriteRule ^(.+)/about/?$ sobre2.php?lang=$1 [L,QSA]
RewriteRule ^(.+)/contact/?$ contato2.php?lang=$1 [L,QSA]
RewriteRule ^(.+)/products/?$ produtos2.php?lang=$1 [L,QSA]
RewriteRule ^(.+)/contact/sales/?$ contato-vendas2.php?lang=$1 [L,QSA]
RewriteRule ^(.+)/contact/general/?$ contato-form2.php?lang=$1 [L,QSA]
RewriteRule ^([a-z]{2}-[a-z]{2})/?$ index2.php?lang=$1 [NC,L,QSA]
I have a couple web pages located at these locations:
Home Page / Index : www.codeliger.com/index.php?page=home
Education : www.codeliger.com/index.php?page=home&filter=1
Skills: www.codeliger.com/index.php?page=home&filter=2
Projects: www.codeliger.com/index.php?page=home&filter=3
Work Experience: www.codeliger.com/index.php?page=home&filter=4
Contact : www.codeliger.com/index.php?page=contact
I am trying to rewrite them to prettier urls:
codeliger.com/home
codeliger.com/education
codeliger.com/skills
codeliger.com/projects
codeliger.com/experience
codeliger.com/contact
I have confirmed that my htaccess file works and mod-rewrite works to google, but I cannot get my syntax working that was specified in multiple tutorials online.
RewriteEngine on
RewriteRule /home /index.php?page=home
RewriteRule /([a-Z]+) /index.php?page=$1
RewriteRule /education /index.php?page=home&filter=1
RewriteRule /skills /index.php?page=home&filter=2
RewriteRule /projects /index.php?page=home&filter=3
RewriteRule /experience /index.php?page=home&filter=4
How can I fix my syntax to rewrite these pages to prettier urls?
The first thing you should probably do is fix your regex. You cannot have a range like [a-Z], you can just do [a-z] and use the [NC] (no case) flag. Also, you want this rule at the very end since it'll match requests for /projects which will make it so the rule further down will never get applied. Then, you want to get rid of all your leading slashes. Lastly, you want a boundary for your regex, otherwise it'll match index.php and cause another error.
So:
RewriteEngine on
RewriteRule ^home /index.php?page=home
RewriteRule ^education /index.php?page=home&filter=1
RewriteRule ^skills /index.php?page=home&filter=2
RewriteRule ^projects /index.php?page=home&filter=3
RewriteRule ^experience /index.php?page=home&filter=4
RewriteRule ^([a-z]+)$ /index.php?page=$1 [NC]
I am working with a domain like:
http://www.domain.com/es/blabla.html
And I want to change the /es part for .es and convert the URLS to something like:
http://www.domain.com.es/blabla.html
I've been trying lots of possibilities but I haven't reached a solution.
For example one of them has been:
RewriteRule ^es/(.*)$ http://www.domain.com.es/$1 [L]
But it has entered in an infinite loop. Which I've tried to avoid adding this lines:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
But the server doesn't allow this calls. So I still have the problem.
Check this.
RewriteEngine On
RewriteRule ^\.html$ /es/blabla.html [L]
I would like to rewrite URL's with htaccess to better readable URL's and use the $_GET variable in PHP
I sometimes make use of a subdomain so it has to work with and without. Also are the variables not necessary in the url. I take a maximum of 3 variables in the URL
the URL sub.mydomain.com/page/a/1/b/2/c/3 should lead to sub.mydomain.com/page.php?a=1&b=2&c=3 and the url sub.mydomain.com/a/1/b/2/c/3 should lead to sub.mydomain.com/index.php?a=1&b=2&c=3 where $_GET['a'] = 1
I came up with this after searching and trying a lot
RewriteEngine on
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4&$5=$6&$7=$8 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3&$4=$5&$6=$7 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4&$5=$6 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3&$4=$5 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)/([^/]+)$ $1.domain.com/$2.php?$3=$4 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)/([^/]+)$ $1.domain.com/index.php?$2=$3 [QSA,NC]
RewriteRule ([^/]+)\.domain.com/([^/]+)$ $1.domain.com/$2.php [L,QSA,NC]
but what I get is an not found server error
I'm not that good at this so maybe I oversee something.
Also I would like it to work with and without a slash at the end
Should I make use of RewriteCond and/or set some options?
Thanks in advance.
When using RewriteRule, you don't include the domain name in the line. Also, make sure you turn on the RewriteEngine first. Like this:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)$ index.php?$1=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?$1=$2&$3=$4
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?$1=$2&$3=$4&$5=$6
The first line will rewrite sub.mydomain.com/a/1 to sub.mydomain.com/page.php?a=1, the second rewrites sub.mydomain.com/a/1/b/2 to sub.mydomain.com/page.php?a=1&b=2, and so on.
I have a few mod_rewrite rules in my htaccess files to redirect inexistent pages to a default page, like this:
http://domain.tld/category1/subcategoryX
http://domain.tld/category1/subcategoryX/
http://domain.tld/category1/subcategoryX/property1
http://domain.tld/category1/subcategoryX/property1/
all redirected to
http://domain.tld/category1/subcategoryX/property1/value1
I'm doing this redirection because the subcategory and property have no page, only the values do.
These are the rules:
RewriteRule ^category1/[a-z-]+$ http://%{HTTP_HOST}%{REQUEST_URI}/property1/value1 [R,NC,L]
RewriteRule ^category1/[a-z-]+/$ http://%{HTTP_HOST}%{REQUEST_URI}property1/value1 [R,NC,L]
RewriteRule ^category1/[a-z-]+/property1$ http://%{HTTP_HOST}%{REQUEST_URI}/value1 [R,NC,L]
RewriteRule ^category1/[a-z-]+/property1/$ http://%{HTTP_HOST}%{REQUEST_URI}value1 [R,NC,L]
The rules are working, but they're kinda verbose. Is there any trick to compact the rules? The difference between the first two is just the end slash, and so is for the last two.
RewriteRule ^(category1/[a-z-]+)/?$ /$1/property1/value1 [R,NC,L]
RewriteRule ^(category1/[a-z-]+/property1)/?$ /$1/value1 [R,NC,L]
or maybe you could even redirect all url starting with category1/*
RewriteRule ^(category1/[a-z-]+) /$1/property1/value1 [R,NC,L]