Writing clean URLS with 2 parameters that point to index.php
You can insert this rule just below RewriteEngine On
to redirect old URL to pretty URL:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?folder=([^\s&]+)&type=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?folder=$1&app=$2 [L,QSA]
Redirect *.php to clean URL
Thanks @Cryo for his help, this is the fix:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.php$ /blah/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /blah/index.php/$1 [L]
Clean URL using PHP or Apache redirect
If the .htaccess is in the share directory then you don't need to specify the URL path. Try something like this:
RewriteEngine On
RewriteRule ^(.*)$ edit.php?id=$1 [QSA,L]
It also goes without saying that should you be using apache you will need mod_rewrite enabled.
Error in htaccess file when clean url of php file
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
It's this rule (that appends the .php
extension via an internal rewrite) that is incorrect.
This results in a rewrite-loop when requesting /single-portfolio/<anything>
because /single-portfolio.php
exists (satisfying the condition) but this rule will incorrectly rewrite the request to /single-portfolio/<anything>.php
, which would ordinarily result in an erroneous 404 (since you are expecting the last rule to catch this request), but due to the L
flag the rewriting engine starts over and repeatedly rewrites the request to /single-portfolio/<anything>.php.php
to /single-portfolio/<anything>.php.php.php
etc. etc. until the server "breaks" and a 500 response is returned.
See my answer to the following question on ServerFault with a more detailed explanation of this behaviour: https://serverfault.com/questions/989333/using-apache-rewrite-rules-in-htaccess-to-remove-html-causing-a-500-error
Simply changing L
to END
will fix the rewrite-loop, but won't resolve the overall problem because you'll just get the erroneous 404 as mentioned above (the request won't be correctly rewritten).
Changing the order of your directives so the last rule that rewrites ^single-portfolio/
is before your second rule that appends the .php
extension (via an internal rewrite) would solve your immediate problem, however, you need to fix the above rule so you are testing the same file you are ultimately rewriting to (otherwise, any URL of the form /php-file-that-exists/<anything>
would result in a rewrite-loop / 500 Internal Server Error response, instead of the expected 404).
For example:
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [L,R]
RewriteRule ^single-portfolio/(\d+)/([\w-]+)$ single-portfolio.php?id=$1&title=$2 [NC,L]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
NB: The shorthand character class \w
is the same as [0-9a-zA-Z_]. And
\dis the same as
[0-9]`.
The NC
flag on the first and last rules are entirely superfluous. I would be wary of using the NC
flag on the ^single-portfolio/
rewrite (it shouldn't be necessary), as this potentially enables duplicate content (eg. /single-portfolio/...
and /SiNgLe-PoRtFoLiO/...
are both valid and resolve to the same resource.)
The first rule that removes the .php
extension via an external redirect should ultimately be a 301 (permanent) redirect once you have confirmed everything is working OK.
Related Topics
Pdo Were Rows Affected During Execute Statement
How to Increment Count in the Replacement String When Using Preg_Replace
How to Bind Decimal/Double/Float Values with Pdo in PHP
Storing Time Information: Timezone Required
Php, Merging Arrays with Common Keys
How to Remove <Br /> Tags and More from a String
How to Get the File Path in HTML <Input Type="File"> in PHP
How to Visualize a Bunch of Functions in Uml
Remove/Replace the Username Field with Email Using Fosuserbundle in Symfony2/Symfony3
How to Keep Whitespace Formatting Using PHP/Html
How to Get User's Screen Resolution with PHP
Forbidden Error When Submitting Simple PHP Form
PHP - Sort Multi-Dimensional Array by Another Array
Override External Product Url to "Add to Cart" Product Button
Calling Perl Script from PHP and Passing in Variables, While Also Using Variablized Perl Script Name
MySQL Pdo Name-Value Prepared Statement Using Last Parameter Only