htaccess url rewrite in dutch language and english language - php

I am handle with htaccess in my subdomain.
My htaccess script is given below
RewriteCond %{HTTP_HOST} ^demo\.example\.com/carrental$
RewriteRule (.*) carrental/([^/.]+)/([^/.]+)/([^/.]+) [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ carrental/locateaddress.php?country=$1&city=$2&locate=$3 [QSA]
RewriteCond %{HTTP_HOST} ^demo\.example\.com/carrental$
RewriteRule (.*) carrental/([^/.]+)/([^/.]+) [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ carrental/city.php?country=$1&city=$2 [QSA]
RewriteCond %{HTTP_HOST} ^demo\.example\.com/carrental$
RewriteRule (.*) carrental/([^/.]+) [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ carrental/country.php?country=$1 [QSA]
I have 2 problems.
I Want to pass special characters in my url.
I am already try (.*) but its getting many problems. I want to pass special characters into ([a-zA-Z0-9_-]+).
My Major Query is i have one url. When my links translate to dutch language.
Url : http://demo.osiztechnologies.com/carrental/Albanië
The problem is because of Albanië. It shows a 404 error. If I change this into English it works fine.
How can I rewrite URL's with special characters?

The %{HTTP_HOST} variable is the HTTP request's "Host:" header. It is only a hostname, no path information is given in that field. Thus:
RewriteCond %{HTTP_HOST} ^demo\.example\.com/carrental$
will never match. Not sure why it's there, as the resulting rule that the condition gets applied to is wrong as well:
RewriteRule (.*) carrental/([^/.]+)/([^/.]+)/([^/.]+) [R=301,L]
Here, you are matching the entire URI (via the (.*)) and then redirecting the browser to:
/carrental/([^/.]+)/([^/.]+)/([^/.]+)
Note those ([^/.]+). They don't get replaced with anything, that's literally where you are sending the browser.
As far as the special characters. Rob Quist is only half right. While they do get encoded by the browser into escape sequences like %C3%AB, the rewrite engine decodes them back into the unicode characters before applying any rules.
So, say you want to include ë, then your rule will look like:
RewriteRule ^([a-zA-Z0-9_-]+)/([ëa-zA-Z0-9_-]+)$ carrental/locateaddress.php?country=$1&city=$2 [QSA]
You can stick all the possible unicode characters you expect to be getting inbetween the square brackets, but you can just make everything match easier by using the groupings similar to the ones in the broken rule:
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)$ carrental/locateaddress.php?country=$1&city=$2&locate=$3 [L,QSA]
RewriteRule ^([^/.]+)/([^/.]+)$ carrental/city.php?country=$1&city=$2 [L,QSA]
RewriteRule ^([^/.]+)$ carrental/country.php?country=$1 [L,QSA]

That won't work. You'd need to replace them with special characters (%C3%AB in this case) in order to work.
The best solution here is to make 2 words for each record - a unique SEO-URL, and the real title.
Let the real title be "Albanië", and the SEO-version "albanie". Just do it for compatibilty's sake.
EDIT: Some browsers (such as Google Chrome) may translate %C3%AB in a URL to ë and back. Too bad the actual request sent to the server is the one with %C3%AB.
EG: Your browser shows: site.com/locations/Albanië
Server receives: GET locations/Albani%C3%AB

Related

htaccess infinite loop issue

I have run into an issue with my .htaccess file.
The file changes the ugly URL such as http://localhost/news.php?article_slug=example-1 to http://localhost/news/example-1
This works perfectly, but when I go to http://localhost/news i get a 404 error.
Within news.php I have a redirect; so if there is not an article slug in the URL it will redirect to latest.php.
my PHP code on news.php
$article_slug=$_GET['article_slug'];
if (empty($_GET)) {
header("Location: ../latest.php");
die();// no data passed by get
}
This is what I currently have in my .htaccsess file
Options -MultiViews
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteRule ^news/([\w\d-]+)$ /news.php?article_slug=$1[QSA,L]
RewriteCond %{QUERY_STRING} article_slug=([\w\d-]+)
RewriteRule ^news$ /news/%1 [R,L]
RewriteRule ^category/([\w-]+)$ /category.php?category_slug=$1&page=$2 [QSA]
When I try to debug this myself (with very little knowledge) and add the following line it redirects to latest.php
RewriteRule ^([^/]*)/?$ /news.php [L,QSA]
but on the redirected page I get the following error
The page isn’t redirecting properly
Firefox has detected that the server is redirecting the request for this
address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept cookies
When I use the developer tools in firefox as IMSoP commented all I see is latest.php reloaded multiple times.
This is not just isolated to just latest.php but any file on the server thats not listed in the access file
when I remove the line
RewriteRule ^([^/]*)/?$ /news.php [L,QSA]
I can load the PHP file but it doesn't redirect from news.php and http://localhost/news is not found but http://localhost/news/example-1 works.
but when I go to http://localhost/news i get a 404 error.
None of your rules catch such a request, so no rewriting occurs and you get a 404.
RewriteRule ^([^/]*)/?$ /news.php [L,QSA]
This rewrites everything to /news.php, including /latest.php that you are redirecting to in your PHP script and the cycle repeats, resulting in a redirect loop to /latest.php.
However, this redirect in your PHP code would also seem to assume there is a slash on the original request. ie. should be /news/ (with trailing slash) not /news (no trailing slash) as you state in the question.
It would be better to redirect to a root-relative (or absolute URL) in your PHP script. ie. header('Location: /latest.php');
RewriteRule ^news/([\w\d-]+)$ /news.php?article_slug=$1[QSA,L]
RewriteCond %{QUERY_STRING} article_slug=([\w\d-]+)
RewriteRule ^news$ /news/%1 [R,L]
(Note you are missing a space before the "flags" argument in the first rule.)
The first rule can be modified to allow news/ and not just news/<something>. This is achieved by simply changing the quantifier from + (1 or more) to * (0 or more) on the capturing subpattern.
The second rule is not currently doing anything. You should probably be targeting news.php here. But the rules are also in the wrong order.
As noted in my answer to your earlier question, the \d shorthand character class is not necessary, since \w (word characters) already includes digits.
Try the following instead:
RewriteCond %{QUERY_STRING} (?:^|&)article_slug=([\w-]*)(?:$|&)
RewriteRule ^news\.php$ /news/%1 [R=301,L]
RewriteRule ^news/([\w-]*)$ /news.php?article_slug=$1 [QSA,L]
Note, the first rule should be a 301 (permanent) redirect, not a 302 (as it was initially). But always test first with a 302 to avoid potential cachining issues.
This allows requests to /news/, but not /news (no trailing slash). Only one of these can be canonical. If you need to handle /news as well then you should redirect to append the trailing slash (so /news/ is canonical, and the URL you should always link to.) For example, before the above two rules:
# Append trailing slash if omitted (canonical redirect)
RewriteRule ^news$ /$0/ [R=301,L]
Summary
Bringing the above points together:
Options -MultiViews
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
# Append trailing slash if omitted (canonical redirect)
RewriteRule ^news$ /$0/ [R=301,L]
RewriteCond %{QUERY_STRING} (?:^|&)article_slug=([\w-]*)(?:$|&)
RewriteRule ^news\.php$ /news/%1 [R=301,L]
RewriteRule ^news/([\w-]*)$ /news.php?article_slug=$1 [QSA,L]
RewriteRule ^category/([\w-]+)$ /category.php?category_slug=$1 [QSA,L]
However, the same now applies to your /category URL. This was also discussed in your earlier question. (I've removed the superfluous &page=$2 part and added the missing L flag.)
If you have many such URLs that follow a similar pattern (eg. news and category etc.) you don't necessarily need a separate rule for each. (An exercise for the reader.)
UPDATE:
$article_slug=$_GET['article_slug'];
if (empty($_GET)) {
header("Location: ../latest.php");
die();// no data passed by get
}
As discussed in comments, this should read:
$article_slug = $_GET['article_slug'] ?? null;
if (empty($article_slug)) {
header("Location: /latest.php");
die(); // no data passed by get
}
Rewrite rules are at heart very simple: they match the requested URL against a pattern, and then define what to do if it matches.
RewriteRule ^([^/]*)/?$ /news.php [L,QSA]
The pattern here translates as "must match right from the start; anything other than a slash, zero or more times; optional slash; must match right to the end". The action if it matches is to act as though the request was for "/news.php", adding on any query string parameters.
That's a very broad pattern; it will match "news" and "news/" but it will also match "hello-world", and "__foo--bar..baz/". The only thing that would stop it matching is other rules higher up your config file.
Meanwhile, every time this rule matches, your PHP code in news.php will run, and if there isn't anything on the query string, will tell the browser to request "latest.php".
But the rule will also match "latest.php". So when the browser requests "latest.php", the code in "news.php" gets run, and tells the browser to request "latest.php" again ... and we have an infinite loop.
The simplest fix is just to make your rule more specific, e.g. look specifically for the word "news":
RewriteRule ^news/?$ /news.php [L,QSA]
Another common technique is to add a condition to the rule that it only matches if the URL doesn't match a real filename, like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?$ /news.php [L,QSA]

.htaccess redirect urls with spaces

I am trying to redirect all invalid urls to my index.php file via my .htaccess file.
Unfortunately I keep getting an Apache error.
My .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^([a-zA-Z0-9\-\_\/]*)$ index.php?p=$1
RewriteRule ^([A-Za-z0-9\s]+)$ index.php?p=$1 [L] 
This invalid url shoud redirect to index.php:
/vacatures/jobapplication/facility-manager%20qsdf
But it throws the object not found 404 Apache error.
The rule you have which allows spaces does not allow hyphens. The rule you have which allows hyphens does not allow spaces. So anything which includes both will not match either.
Your invalid URL facility-manager%20qsdf includes both.
My guess is that your RewriteCond is supposed to apply to both rules, but that is not what is happening now, it will apply only to the first RewriteRule after it. You can solve all these problems by including just 1 RewriteRule, and amending it to accept everything you want:
RewriteRule "^([A-Za-z0-9\-\_\/\s]+)$" index.php?p=$1 [L]
Note that this requires at least one of the characters in your character class, in other words it will not match your "home" location when there is no path ("http://somewhere.com/"). If you want to also match for that location, change the + to a *, to allow 0 or more character matches.
Your rewrite rules do not match the url you indicated. Your REQUEST_URI is
/vacatures/jobapplication/facility-manager%20qsdf
I suspect the URL decoding is not done before the RewriteRule matching and therefore it's trying to match literally %20, yet % sign is not included in your match. I'm not sure why you're using two RewriteRules - why not do something like this?
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteCond %{REQUEST_URI} !^index.php(\?.*)?$
RewriteRule ^(.*)$ index.php?p=$1 [L]

URL Rewriting .php to .html

I'm converting my urls extension from .php to .html in my .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} (.).php
RewriteRule ^(.*).php $1.html [R=301,L]
RewriteRule ^(.*).html $1.php [L]
FallbackResource /index.php
The problem is that I have some section with the word "php" on it:
www.mywebsite.com/phpscripts.php
And when it is converted:
www.mywebsite.com/htmlscripts.html
^(.*).php
This regex says: anything, including nothing, followed by a single arbitrary character, followed by "php". For example, it'll match "blahphp.html", specifically it'll match the "blahphp" part and not care about the extension at all.
What you're looking for is this:
RewriteRule (.+)\.php$ $1.html [R=301,L]
RewriteRule (.+)\.html$ $1.php [L]
(.+)\.php$ is something (at least one character) followed by a period followed by "php" at the end of the string. You can also get rid of the RewriteCond, it doesn't add anything to these rules.
Also note that you should be changing all your HTML files to link to href="...html". Don't rely on these redirects alone to fix your problem; not only is it inefficient to redirect every single request, it'll also break POST requests. It's only acceptable to redirect clients which try to open the old URLs for whatever reason to the new canonical URLs.

Rewrite automatically removes backslash if there's more than one?

I have a very simple url rewriting rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} !script.php
RewriteRule ^test/(.*) script.php?q=$1
The idea is to have this kind of urls: http://mywebsite.com/test/http://example.com
and then send http://example.com to the script.php as a query parameter. The problem is that I'm receiving http:/example.com instead of http://example.com. Also, http:////example.com would be sent as http:/example.com. What causes this behavior ?
Apache mod_rewrite engine converts multiple ///... into single / for pattern matching in RewriteRule directive. However if you match it using RewriteCond then you can match multiple /s.
You can use rule like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/+test/+(https?://.+)$ [NC]
RewriteRule ^ script.php?q=%1 [L,QSA]
The browser causes this behaviour. It contracts a sequence of / into 1 /, because it is still essentially a path. ///// does not change the directory we are in, so we could as well use /.
You have two options:
Change your links to use a query string instead. If you rewrite test/?q=something to script.php?q=something everything works as expected. You would do the following:
RewriteRule ^test/?$ script.php [L]
Since you don't alter the query string, the original query string is automatically copied to the new query string.
Don't make an assumption on how many slashes you will encounter. The url might not look correctly in the url bar of the browser, but if it is just a redirect, it will only be visible for a very short period of time.
RewriteRule ^test/(http|https):/+(.*)$ script.php?q=$1://$2

redirect adding %20HTTP to end of the url

Im redirecting this:
example.com/category/name-of-title/number
to this:
example.com/name-of-title/number
Everything is working perfect except its adding %20HTTP to the end of the url after the redirect like so:
example.com/name-of-title/number%20HTTP
Here is my code:
RewriteCond %{THE_REQUEST} /category/(.*)/(.*) [NC]
RewriteRule ^ /%1? [R=301,L]
Because as the documentation states, THE_REQUEST is
“[t]he full HTTP request line sent by the browser to the server (e.g., "GET /index.html HTTP/1.1")”
– so in your case, that would be something like GET /category/name-of-title/number HTTP/1.1, and applying the pattern /category/(.*)/(.*) onto that results in exactly the part name-of-title/number HTTP as the first match (and the remaining 1.1 as the second), because of the greediness of regular expressions.
It is nonsense anyway to use a RewriteCond and the value THE_REQUEST for this – a simple RewriteRule like this should work perfectly fine:
RewriteRule ^category/(.*)/(.*)$ /$1/$2 [R=301,L]
And if you want to match anything that could be behind category/ anyway, you don’t even need two capturing subpatterns, but could simply use
RewriteRule ^category/(.*)$ /$1 [R=301,L]
%20http is an empty space!
Remove the space, and this %20http will no longer appear.

Categories