2

I've got the following htaccess file:

RewriteEngine On
RewriteBase /

# Redirect to remove .php 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

# Redirect to "page" for dynamic pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ page?url=/$1 [L]

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]

This allows my custom CMS to use dynamic URLs (http://example.com/some-page, for example) and redirect it to http://example.com/page?url=some-page so that the CMS can render the content. It all works great - until someone adds a URL like http://example.com/something/else. When I spit out the url parameter with: print $_GET['url']; I get /something/else.php/else.

So it seems like the remove .php directive is getting lost and the second parameter is getting duplicated? Thanks for any help.

5
  • Could removeing the .php from the string be a solution? Commented Aug 4, 2017 at 20:18
  • The first rule should be doing that - and I know that one works as I've got a few static pages that display fine (without the .php). Commented Aug 4, 2017 at 20:30
  • "page?url=/$1 [L]" - Shouldn't that be page.php? (Otherwise, you are relying on mod_rewrite to add the extension later with another rewrite.) Commented Aug 4, 2017 at 20:31
  • Hmm, tried that as well, no luck - same problem. Commented Aug 4, 2017 at 20:42
  • 1
    (That wasn't intended as a solution, but it avoids an unnecessary rewrite.) Is /something a physical directory, or entirely virtual? Does /something/else.php exist? Commented Aug 4, 2017 at 21:04

1 Answer 1

2

Have it this way:

Options -MultiViews
RewriteEngine On

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,NE,L]

RewriteCond %{ENV:REDIRECT_STATUS} =200
RewriteRule ^ - [L]

# Redirect to remove .php 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]

# Redirect to "page" for dynamic pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule !^page\.php$ page.php?url=%{REQUEST_URI} [L,NC,QSA]

Here are changes:

  1. Keep redirect rule before rewrite rules otherwise when www is removed from a URL then your internal URL will be exposed.
  2. Use page.php in target instead of page to avoid another rewrite rule execution.
  3. Use [L] flag in .php adding rule.
  4. Addition of Options -MultiViews
Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately that throws a 500 internal server error - but only on pages with multiple URL parameters (/something/else). Pages with a simple /something URL are fine.
Same problem - page.php is in site root (/public_html/page.php).
Yes, just did that - looks like the error is "AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace."
Done, same problem unfortunately. There are some subdirectories in the site but page.php is in root.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.