I have a route like this in api.php:
...
Route::resource("infos",PaymentController::class)->only([
'show','store'
]);
...
And when I call my API like these:
/api/infos/ABC123 => success with Status Code: 200 (in access.log)
/api/infos/ABC123/ => there area 2 log (Status code: 301; then Status code 200)
/api/infos/ABC123//// => there area 2 log (Status code: 301; then Status code 200)
Why when I add slash symbols, there are 2 line in access.log?
Thanks!
The .htaccess file that ships with Laravel has a section in it that strips trailing slashes by using a redirect.
This is from the .htaccess file, which can be found here:
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
So, the first line you're seeing is the redirect triggered by this .htaccess code, and the second line is the final request without the slash.
Because when you add / then Laravel redirect it to address without /.
Related
I'm getting warning in my CodeIgniter Application.
A PHP Error was encountered
Severity: Warning
Message: strpos(): Empty needle
<!-- <p>Filename: core/URI.php</p>
<p>Line Number: 191</p> -->
<p>Line Number: 187</p> -->
My apache conf settings look like
RewriteCond $1 !^(index\.php|robots\.txt|fonts|assets|uploads|images)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
The code at CodeIgniter core/URI.php looks like
private function _detect_uri()
{
if ( ! isset($_SERVER['REQUEST_URI']) OR ! isset($_SERVER['SCRIPT_NAME']))
{
return '';
}
$uri = $_SERVER['REQUEST_URI'];
187 -> if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0)
{
$uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
}
elseif (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0)
{
$uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
}
...}
It is obvious that SCRIPT_NAME is empty. However, why it is getting into line 187? isset() function would have returned right above. Isn't it?
I saw some answer that my Rewrite Conf might be the problem. However, I do not see anything wrong with it.
Please help me solve it out.
Thanks.
Happy New Year.
You need to put your rewrite rules in an .htaccess file rather than the VirtualHost section of your site conf file. isset() will still return TRUE for something set to an empty string. Either that or modify the core file to check if SCRIPT_NAME is set OR an empty string on line 181.
Apparently, this has something to do with an age old bug that is either Apache's fault or PHP's, but neither seems to accept responsibility. Putting your rewrite in an .htaccess file will properly set the SCRIPT_NAME to index.php.
To enable a shorten-url service, I had to add a rewrite rule to my
lighttpd.conf. This seems to be important, otherwise shortlinks like
http://example.com/Xf6Y would return a 404 error. This is because Xf6Y
is not a file but a shortlink hash.
So I added these lines to my lighttpd.conf:
url.rewrite-if-not-file = (
"^/(.*)$" => "/index.php?fwd=$1"
)
This works excellently, except for websites that contain a index.html
instead of an index.php.
I tried to duplicate the rewrite rule, like url.rewrite-if-not-file = (
"^/(.)(\?.)?$" => "/index.php?fwd=$1",
"^/(.)(\?.)?$" => "/index.html?fwd=$1"
)
This resulted in a "duplicate rule" error, so lighttpd is
not able to restart.
Does anyone know how to write a rewrite rule that will redirect index.php and index.html?
I'm using CodeIgniter and I want to redirect links like this:
example.com/?p=25
to this:
example.com/25
How can this be achieved?
http://www.askaboutphp.com/58/codeigniter-mixing-segment-based-url-with-querystrings.html
or
to create url like this : http://yyyy.com/article/finishing-dan-snapshop-salkulator
add this code in routes.php
$route['article/(:any)'] = "article/readmore/$1";
description :
1. article : class name
2. readmore : method from class article
3. $1 : get value from uri segment 2 value
its .htaccess rewrite rule.make sure u have activated mod_rewrite .then put this line into website application root .htaccess file
RewriteRule ^/([0-9]+)/?$ p=$1 [NC,L] # Handle product
requests
I'm trying to implement nginx rewrite rules for the following situation
Request:
http://192.168.64.76/Shep.ElicenseWeb/Public/OutputDocuments.ashx?uinz=12009718&iinbin=860610350635
Should be redirected to:
http://localhost:82/Public/OutputDocuments.ashx?uinz=12009718&iinbin=860610350635
I tried this with no luck:
location /Shep.ElicenseWeb/ {
rewrite ^/Shep.ElicenseWeb/ /$1 last;
proxy_pass http://localhost:82;
}
What is the correct way to perform such a rewrite for nginx ?
Your rewrite statement is wrong.
The $1 on the right refers to a group (indicated by paratheses) in the matching section.
Try:
rewrite ^/Shep.ElicenseWeb/(.*) /$1 break;
You're missing a trailing slash:
location /Shep.ElicenseWeb/ {
proxy_pass http://localhost:82/;
}
This will work without a rewrite.
I'm edditing my htaccess to internally redirects
just about any URL to a php page handler:
RewriteRule ^images\/ - [L,NS]
RewriteRule ^docs\/ - [L,NS]
RewriteRule ^([A-Za-z0-9\_\-]+)\/?$ pages/pagehandler.php?page=$1 [L,QSA,NS]
this works fine accept that directories that are enteres in the address
bar with no trailing slash for some reason get duplicate query strings,
and for some reason the address bar of the browser changes
for example, if I type the URL:
localhost/movies
if there's a directory called movies in the site root
than the address changes to:
localhost/movies/?page=movies
I guess this is some collision with mod_dir but I don't know
how to overcome it, if I use:
<IfModule mod_dir.c>
DirectorySlash Off
</IfModule>
Than it works, but I don't want this, I think for some reason the url
is rewritten than mod dir changes it and than it is rewritten again thus
making duplicate query strings,
Any Ideas?
EDIT: I Add a relevant part of the Rewritelog, this is all from one request:
strip per-dir prefix: /opt/lampp/htdocs/movies -> movies
applying pattern '^images\/' to uri 'movies'
strip per-dir prefix: /opt/lampp/htdocs/movies -> movies
applying pattern '^docs\/' to uri 'movies'
strip per-dir prefix: /opt/lampp/htdocs/movies -> movies
applying pattern '^pages\/' to uri 'movies'
strip per-dir prefix: /opt/lampp/htdocs/movies -> movies
applying pattern '^([A-Za-z0-9\_\-]+)\/?$' to uri 'movies'
rewrite 'movies' -> 'pages/pagehandler.php?page=movies'
split uri=pages/pagehandler.php?page=movies -> uri=pages/pagehandler.php, args=page=movies
add per-dir prefix: pages/pagehandler.php -> /opt/lampp/htdocs/pages/pagehandler.php
trying to replace prefix /opt/lampp/htdocs/ with /
strip matching prefix: /opt/lampp/htdocs/pages/pagehandler.php -> pages/pagehandler.php
add subst prefix: pages/pagehandler.php -> /pages/pagehandler.php
internal redirect with /pages/pagehandler.php [INTERNAL REDIRECT]
strip per-dir prefix: /opt/lampp/htdocs/movies/ -> movies/
applying pattern '^images\/' to uri 'movies/'
strip per-dir prefix: /opt/lampp/htdocs/movies/ -> movies/
applying pattern '^docs\/' to uri 'movies/'
strip per-dir prefix: /opt/lampp/htdocs/movies/ -> movies/
applying pattern '^pages\/' to uri 'movies/'
strip per-dir prefix: /opt/lampp/htdocs/movies/ -> movies/
applying pattern '^([A-Za-z0-9\_\-]+)\/?$' to uri 'movies/'
rewrite 'movies/' -> 'pages/pagehandler.php?page=movies'
split uri=pages/pagehandler.php?page=movies -> uri=pages/pagehandler.php, args=page=movies&page=movies
add per-dir prefix: pages/pagehandler.php -> /opt/lampp/htdocs/pages/pagehandler.php
trying to replace prefix /opt/lampp/htdocs/ with /
strip matching prefix: /opt/lampp/htdocs/pages/pagehandler.php -> pages/pagehandler.php
add subst prefix: pages/pagehandler.php -> /pages/pagehandler.php
internal redirect with /pages/pagehandler.php [INTERNAL REDIRECT]
Also the relevant part from the access log:
"GET /movies HTTP/1.1" 301
"GET /movies/?page=movies HTTP/1.1" 200
Can you change your rewrite rule to:
RewriteRule ^([A-Za-z0-9\_\-]+)/?$ /pages/pagehandler.php?page=$1 [L,QSA,NS]
Note the / before pages.
For anyone having this problem, what I did ultimately in disable the Directory Slash:
<IfModule mod_dir.c>
DirectorySlash Off
</IfModule>
And I used a RewriteRule to 301 Redirect all directory requests than do not end with a /
I guess this is some sort of collision between mod_rewrite and mod_dir
as I primarily thought
And I used a RewriteRule to 301 Redirect all directory requests than do not end with a /
I had a similar problem but could not find a solution over mod_rewrite (did not know how to add this slash with a 301 redirect - tried everything).
What I did is to define:
<IfModule mod_dir.c>
DirectorySlash Off
</IfModule>
ErrorDocument 404 404.php
If now the trailing slash is missed, 404.php is used redirecting the client if the url has no trailing slash now:
<?php
$home="http://".$_SERVER['HTTP_HOST'];
$url=$home.$_SERVER['REQUEST_URI'];
?><html>
<head>
<title><?php echo $_SERVER['REDIRECT_STATUS']; ?></title>
<?php if(!preg_match('/^.+\/$/', $url)) { ?>
<meta http-equiv="refresh" content="0; URL=<?php echo $url; ?>/">
<? } ?>
</head>
<body>
<h1><?php echo $_SERVER['REDIRECT_STATUS']; ?></h1>
</body>
</html>
It smells like hell but worksforme ...