Mobile app version of vmapp.org
Login or Join
Alves908

: 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,

@Alves908

Posted in: #Htaccess #HttpCode500 #UrlRewriting

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]

##

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Alves908

2 Comments

Sorted by latest first Latest Oldest Best

 

@Phylliss660

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]

10% popularity Vote Up Vote Down


 

@Gloria169

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).

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme