Our URLs with a URL encoded trailing white space (%20) are producing a 404 error. The application is run on Codeigniter on Apache.
/directory/page%20 will return a 404 error
/directory/page will return a 200 OK
How can I route all URLs with a trailing %20 to the intended URL?
The problem is that some third party websites are linking to us with trailing white space in the HREF
In that case you can add something like the following at the top of your .htaccess file to redirect (canonicalise) such requests to remove the trailing space.
For example, before the Codeigniter front-controller:
RewriteCond %{REQUEST_URI} \s$
RewriteRule (.*) /$1 [R=302,L]
The "processed" URL-path matched by the RewriteRule pattern has already had the trailing slash removed, however, the REQUEST_URI server variable has not. So, we can check for the trailing space on the REQUEST_URI and simply redirect to "the same" (processed) URL-path, as captured by the RewriteRule pattern.
The REQUEST_URI server variable is already %-decoded. The \s shorthand character class matches against any whitespace character and the trailing $ anchors this to the end of the URL-path.
Test first with a 302 (temporary) redirect to make sure that it works OK before changing to a 301 (permanent) redirect.
#Juan Cullen, this is a common issue when you have a space before a trailing slash. For example lets say: "http://example.com/directory/page /". you can notice the space before the trailing slash.
To solve this for all urls that have such behavior, you can use PHP's rtrim() function.
Check the code below
<?php
function fix_url($url) {
$trailing_slash = ' /'; //notice a space before the slash
return $url = rtrim($url, $trailing_slash);
}
Now you can call it like this:
$error_url = "http://example.com/directory/page /";
$correct_url = fix_url($error_url);
As you are using Codeigniter, you can put this function in a helper file and access wherever you want.
This is an Idea try it out and let me know if it works.
Related
I have the following htaccess rule to rewrite my URL
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^app/(.*)/(.*)/(.*)$ app/index.php?app_released=$1&app_name=$2&app_version=$3 [L,NC]
</IfModule>
Which rewrites to app/3-25-2018/name/version that's fine, but when user tries to put more slashes and some random values after the slash for example app/3-25-2018/name/version/something/else/here it should redirect them to my custom 404 page but I'm receiving undefined app_released error by PHP because, it's not able to read the GET variable properly. How can I fix this?
The problem lies in the regular expression you use and particularly (.*), which matches all characters including /. Instead, supplant (.*) with ([^\/]*) which matches all characters besides /.
Correct Regex:
^app\/([^\/]*)\/([^\/]*)\/([^\/]*)$
See the above regular expression in action in the following examples:
matching app/3-25-2018/name/version - here.
not matching app/3-25-2018/name/version/something/else/here - here.
I have the following re-write rule:
RewriteRule ^([\w-]+)$ modules.php?mod_name=$1
It allows me to open a url like:
mydomain.com/settings
This calls the php file in the following format:
modules.php?mod_name=settings
However, I need to call my php script with mod_name values that contain a path e.g:
modules.php?mod_name=settings/preferences.php?id=1103
I would like the re-write rule to be able to accept the above in the following format:
mydomain.com/settings/preferences/1103
Any ideas how this can be done?
RewriteRule ^([\w-]+)$ modules.php?mod_name=$1
Your \w matches word characters ([a-z] [A-Z] [0-9]) and dashes (-) (thus not the /).
To include the /, you need something like ^([\w-/]+)$
I am a bit confused on why it returns incorrect parameter when the url ends with a slash.
htaccess
RewriteRule ^account/dashboard/(.*)/(.*)$ ./account/index.php?page=dashboard&aid=$1&name=$2 [L,QSA]
when I execute http://example.com/account/dashboard/65/blitzen12/
and in the page i can use $_GET['aid] it returns 65/blitzen12 and $_GET['name] returns empty
but when I remove slash at the end of blitzen12 in the url it returns correctly which is 65 and blitzen12.
can anyone explain to me what I did wrong?
you should do it like this:
RewriteRule ^account/dashboard/([^/]+)/([^/]+)/?$ ./account/index.php?page=dashboard&aid=$1&name=$2 [L,QSA]
it has to do with greedy repetition. Basically, the dot matches any character, including the slash(/)
We need to create a rule on htaccess that will redirect a page according to the url. if the path or last part of the url has only letters or numbers, no underscores, dots, slashes or dashes, we want to use that string found and send it as a parameter to another php page that will decide how to use it.
Something like this:
suppose that /^([^\.,\-,\_,\/]+) means all except '.', '-', '_', '/' and spaces
RewriteRule /^([^\.,\-,\_,\/]+)$ handler.php?alias=$1 [L]
where $1 will give us a string with letters and numbers only.
For example:
mysite.com/someone = handler.php?alias=someone
and
mysite.com/styles.css or mysite.com/images/logo.jpg or mysite.com/page_2, etc
will not be redirected.
I am really lost with regular expressions. Any Ideas?
Thanks!
Have you tried this ?
RewriteRule ^([a-zA-Z0-9]+)$ handler.php?alias=$1 [L]
or
RewriteRule ^([a-z0-9]+)$ handler.php?alias=$1 [NC,L]
or
RewriteRule ^[a-z0-9]+$ handler.php?alias=$0 [NC.L]
try
RewriteRule ^.*([^\/\.\-_]+)$ handler.php?alias=$1
[Edit]: If you don't want to match whitespace (that should never be present in URLs, so that shopuldn't even make a difference, but still ^^), do
RewriteRule ^.*(\w+)$ handler.php?alias=$1
I'm trying to forward example.com/signup and example.com/signup/ (with trailing slash) to example.com/signup.php
I wrote following to .htaccess and it works for example.com/signup/ but doesn't work without trailing slash... How can I solve the problem?
RewriteEngine On
RewriteRule ^question/(.*)-(.*) /question.php?qid=$1
RewriteRule ^signup/ /signup.php
RewriteEngine On
RewriteRule ^signup/?$ /signup.php
The question mark makes the slash optional, and the dollar sign means end-of-line, so nothing can follow. You can remove the $ if you like.
You could also allow arbitrary query strings at the end of the URL with something like:
RewriteRule ^signup/?(\?.*)?$ /signup.php$1
That would allow you to pass any query parameters on to your PHP script. For example the URL http://www.example.com/signup?destination=/front-page would be redirected to http://www.example.com/signup.php?destination=/front-page.
Make the slash optional with the ? quantifier:
RewriteRule ^signup/?$ /signup.php
But I recommend you to just use one notation and redirect the other one.
Put a question mark after the slash
RewriteRule ^signup/? /signup.php
? = optional single character in regex matches