JosephSilber / page-cache

Caches responses as static files on disk for lightning fast page loads.
MIT License
1.21k stars 118 forks source link

index.php shown in url for cached pages #18

Open julianchristmas opened 6 years ago

julianchristmas commented 6 years ago

First of all, thanks for this great package.

index.php shown in url

I have tested it on our simple Laravel website and found a little quirk. When visiting a cached page, the displayed url includes index.php This is not beneficial for SEO and UX, therefore I would like to change it.

Have anyone else experienced this issue and found a work around?

Prior to installing this package, urls where in the form of https://app_url.com/route

After installation and visiting all package to warm the cache, the urls are now in the form of https://app_url.com/index.php/route

Outcommenting the recommended lines from .htaccess resolves the problem, but views are no longer served from cache.

Apache configuration

Apache running with PHP 7.2 The full .htaccess file is as follows

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Serve Cached Page If Available...
    RewriteCond %{REQUEST_URI} ^/?$
    RewriteCond %{DOCUMENT_ROOT}/page-cache/pc__index__pc.html -f
    RewriteRule .? page-cache/pc__index__pc.html [L]
    RewriteCond %{DOCUMENT_ROOT}/page-cache%{REQUEST_URI}.html -f
    RewriteRule . page-cache%{REQUEST_URI}.html [L]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

# BEGIN Expires-Headers
<IfModule mod_expires.c>
  <FilesMatch "\.(js|css)$">
    ExpiresActive On
    ExpiresDefault "access plus 4 weeks"
  </FilesMatch>
</IfModule>
# END Expires-Headers

# BEGIN Cache-Control-Headers
<ifmodule mod_headers.c>
  <filesmatch "(gif|ico|jpeg|jpe|jpg|svg|png|css|js)$">
    Header set Cache-Control "max-age=604800, public"
  </filesmatch>
</ifmodule>
# END Cache-Control-Headers
gauravmak commented 6 years ago

Yes, we faced the same issue.

The solution was to put the redirection code as per https://stackoverflow.com/a/21813759/3113599 It's for nginx though.

sebastiandedeyne commented 6 years ago

I have the same issue. Using nginx. Is this a configuration problem or something the package can solve?

alexjoffroy commented 6 years ago

Using Nginx, I fixed the issue in my site config (thanks to @gauravmak 's link).

if ($request_uri ~* "^(.*/)index\.php(\/?)(.*)") {
    return 301 $1$3;
}
william1888 commented 6 years ago

Is there a recommended solution for apache?

dieg0v commented 5 years ago

I think you can use a 301 redirect in apache, after "Serve Cached Page If Available" condition, maybe you have to delete the entire cache first, something like this:

RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php|html)(.*)\ HTTP
RewriteRule ^(.*)index\.(php|html)$ /$1 [R=301,L]
rajeshtandukar commented 4 years ago

Still index.php shown in url for cached pages with Laravel 5.5 ( Apache Server) Any solution plz?

bmpf commented 2 years ago

For those who are using apache , Add this two line after RewriteEngine On in .htaccess

RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

*clear the cache after.