I have this .htaccess
file and I have no knowledge with mod_rewrite,
RewriteEngine On
RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} s/([^.]+).php [NC]
RewriteRule ^ /%1 [NE,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /$1.php [L]
What I want to achieve is to have localhost/viewticket/${id}
instead of localhost/viewticket.php?id=123
I have tried many .htaccess
rules but none worked except this one that hides .php
in my URL.
3
Answers
Make sure you’ve changed your internal URLs so you are linking to
/viewticket/<id>
. Then add the following at the top of the root.htaccess
file:If you are not expecting any query string on your "pretty"
/viewticket/<id>
URL then remove theQSA
(Query String Append) flag in the last rule.$1
is a backreference to the first capturing group (parenthesised subpattern) in theRewriteRule
pattern. Likewise,%1
is a backreference to the first capturing group in the last matched CondPattern (RewriteCond
directive).Always test first with 302 (temporary) redirects to avoid potential caching issues and only change to a 301 (permanent) redirect when you are sure everything is working as intended. Clear your browser cache before testing.
Reference:
Additional:
Your existing directives (that remove the trailing slash and
.php
extension) are not quite correct.Your first rule that unconditionally removes the trailing slash will also "try" to remove the trailing slash from directories as well – which will conflict with mod_dir and result in a redirect loop. The negated character class
[^/]
(to match a non-slash character) in the middle of the regex would seem to be superfluous. This also redirects to HTTP (not HTTPS)?The second rule that removes the
.php
extension would also erroneosuly match.php
should it appear in the query string portion of the URL. eg./foo?bar.php
would be redirected to/foo?bar
. Although the regex([^.]+)
does avoid the perhaps more common case of/foo.php?bar.php
being erroneously redirected.The third rule that appends the
.php
extension via an internal rewrite could potentially result in a rewrite-loop (500 Internal Server Error) if you received a request of the form/foo/bar
and/foo.php
exists in the root of the filesystem. Also, the (optional) trailing slash is also captured by the capturing subgroup, since the*
quantifier is greedy. This would also result in a malformed rewrite, if it wasn’t for the first rule that removes the trailing slash. (But since the trailing slash is removed then the optional trailing slash in this rule is redundant.)If you still need these rules to remove the trailing slash and handle extensionless
.php
URLs then have these rules as follows. Make sure you are already linking to the extensionless URLs internally. These rules should follow the rule block I posted above.These directives (and your original directives for that matter) do assume the
.htaccess
file is located in the document root directory.You can use the following code:
And if it won’t works you can read this PDF file: How to remove PHP or HTML extensions with htaccess
I created this one for you, just erase yours and copy this one enjoy^^