: Rewrite rule to show as directory using .htaccess I want to implement a rewrite rule in my .htaccess file to show a specific url as a directory of my server. See the code below I written,
I want to implement a rewrite rule in my .htaccess file to show a specific url as a directory of my server. See the code below I written,
RewriteRule ^(.*)/$ ?page= [NC]
This will rewrites urls like mysite.com/abc/ to mysite.com/index.php?page=abc. But if I request mysite.com/abc then it is throwing an 404 error.
How can I write a rewrite rule which will match mysite.com/abc and mysite.com/abc/ both?
Edit:
My current .htaccess file (After Litso's answer's 3rd revision) is like below:
##
ErrorDocument 401 /index.php?error=401
ErrorDocument 400 /index.php?error=400
ErrorDocument 403 /index.php?error=403
ErrorDocument 500 /index.php?error=500
ErrorDocument 404 /index.php?error=404
DirectoryIndex index.htm index.html index.php
RewriteEngine on
RewriteBase /
Options +FollowSymlinks
RewriteRule ^(.+).html?$ .php
RewriteCond !-d
RewriteRule ^(.*)/$ ?page= [NC,L]
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^(.*)$ ?page= [NC,L]
##
More posts by @Alves908
2 Comments
Sorted by latest first Latest Oldest Best
The following conditions and the rule should do the trick for you. You can chain RewriteCond directives with logical AND (default) or logical OR (by appending [OR]).
# Check that there's no directory with that name
RewriteCond !-d
# Check that there's no regular file with that name
RewriteCond !-f
# Prevent loop if index.php is requested
RewriteCond %{REQUEST_URI} !index.php
# Finally rewrite the request; trailing slash optional
RewriteRule ^(.*)/?$ index.php?page= [NC,L]
update
RewriteCond !-d
RewriteRule ^(.*)/$ ?page= [NC,L]
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^(.*)$ ?page= [NC,L]
This first rewrites urls with a slash, but only if it's not an existing directory.
If that's not found it rewrites urls without a slash (but only if it's not index.php, so already rewritten urls are ignored).
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.