'removing .php extension and creating dynamic url from htaccess
I have following code for hiding php extension and redirecting user to a non .php url if user adds a .php extension in url for example domain.com/about.php should go to domain.com/about
RewriteBase /
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
But the problem is that I want to create dynamic url where domain.com/abc?slug=fased should be domain.com/abc/fased but its not working what to do I have used the code RewriteRule ^abc/([^.]+)$ abc?slug=$1 and RewriteRule is on
Solution 1:[1]
Try it like this instead in the root .htaccess file:
Options -MultiViews
RewriteEngine On
# Redirect to remove ".php" and optional "slug" query string
RewriteCond /%{QUERY_STRING} ^(?:(/)(?:slug=([^/&]+))|/.*)$
RewriteRule ^([^./]+)\.php$ /$1%1%2 [QSD,R=301,END]
# Redirect to remove "slug" query string (when ".php" is not present on URL-path)
RewriteCond %{QUERY_STRING} ^slug=([^/&]+)$
RewriteRule ^([^./]+)$ /$1/%1 [QSD,R=301,END]
# Rewrite to append ".php" extension
# Handles both "/about" (empty "slug") and "/abc/fased"
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^./]+)(?:/([^/]+))?$ $1.php?slug=$2 [END]
In the directives you posted the RewriteBase directive was superfluous.
This handles both scenarios. ie. Requests for /about and /abc/fased. Requests for /about are internally rewritten with an empty slug URL parameter. However, this also means that /about/foo would also be a valid request (and serve /about.php). But that is really an issue with your "generalised" URL structure not these directives.
The first two rules handle the canonical redirects for SEO (you should already be linking to the canonical URLs internally). For example:
/about.phpredirects to/about/abc?slug=fasedredirects to/abc/fased/abc.php?slug=fasedredirects to/abc/fased
However, if any other URL parameters are present on the request then the condition won't match and there will be no redirect.
Clear your browser cache before testing and test first with a 302 (temporary) redirect to avoid potential caching issues.
Solution 2:[2]
There's a really good set of introductions to rewrite rules here: Reference: mod_rewrite, URL rewriting and "pretty links" explained
One thing that's really useful to remember is that rewrites don't "make URLs pretty". The pretty URL is the input, and the ugly URL underneath is the output.
Redirects are the other way around, but they just tell the user to go somewhere else; the somewhere else has to work first.
With that in mind, your requirements are this:
- a request for
abc/fasedshould serveabc.php?slug=fased; and so on for any "slug" - a request for
defshould servedef.php, and so on for anything matches a PHP file and isn't already covered by rule 1 - a request directly for
def.phpshould tell the user to go todefinstead
So:
# Rule 1
RewriteRule abc/(.*) abc.php?slug=$1 [END]
# Rule 2
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [END]
# Rule 3
RewriteRule ^(.+)\.php$ /$1 [R,END]
Only one rule can match each request, because they all have the END flag, and they'll be processed in order. You could put Rule 3 first, but it will only make a difference if you have a file called "abc/something.php" or "something.php.php".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | IMSoP |
