There's quite a bit of content available on mod_rewrite but I simply haven't been able to get the following to work.
Right now I have the following code in my htaccess file:
RewriteEngine On
Options -Indexes
Options -Multiviews
ErrorDocument 400 http://localhost/mywebsite/400.php
ErrorDocument 403 http://localhost/mywebsite/403.php
ErrorDocument 404 http://localhost/mywebsite/404.php
ErrorDocument 500 http://localhost/mywebsite/500.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
Which will succesfully make things like mywebsite/index work(instead of mywebsite/index.php).
However there are some situations where I will have visitors who will access the site with a trailing slash(mywebsite/index/) and then my rewrite no longer functions. and I receive an error page.
So how will I make it work so any page with either a trailing slash, no slash, or an extension(.php) will work on my website?
I will add that this is on localhost on an apache server(XAMPP) in case this affects how mod_rewrite works.
Replace your code with this :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
to
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ([^/]+)/?$ $1.php [L]
You can use:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+)/?$ $1.php [L]
Because you can't test before if file (without /) exist or not.
A rewrite rule like this should do the trick:
RewriteRule ^(.*)/?$ $1.php [L]
Related
I have made sure that rewrite engine is enabled and removing .php extensions is working so I know that isn't the issue.
what I'm trying to do is simply remove the ?id=value aspect of the URL, so basically making the URL look like such:
folder/medias/value
Instead of
folder/medias?id=value
My current .htaccess looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteRule ^404/?$ /404.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ 404.php [L,R]
With your shown samples/attempts, please try following htaccess Rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules for external rewrite.
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?id=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Rule for internal rewrite.
RewriteRule ^([^/]*)/([^/]*)/?$ $1?id=$3 [L]
You may try this code inside the /folder/.htaccess (create this file if it doesn't exist):
RewriteEngine On
# External redirect from /folder/media?id=val to /folder/media/val
RewriteCond %{THE_REQUEST} /(\S+?)\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ /folder/%1/%2? [R=301,L,NE]
# Internal rewrite from /folder/media/val to /folder/media?id=val
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ $1.php?id=$2 [L,QSA]
Trailing ? in first rule is to remove query string from original URL.
%{REQUEST_FILENAME} !-f and %{REQUEST_FILENAME} !-d is to skip existing files and directories from rewrite in 2nd rule.
Recently i Modified my .htaccess file in my apache server. and this is the following code
RewriteEngine On
<IfModule mod_speling.c>
CheckSpelling On
</IfModule>
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^course/([^/.]+)$ course.php?name=$1 [QSA,L]
RewriteRule ^course/([^/.]+)/$ course.php?name=$1 [QSA,L]
RewriteRule ^about$ about.php [QSA,L]
RewriteRule ^about/$ about.php [QSA,L]
RewriteRule ^contact$ contact.php [QSA,L]
RewriteRule ^contact/$ contact.php [QSA,L]
RewriteRule ^downloads$ downloads.php [QSA,L]
RewriteRule ^downloads/$ downloads.php [QSA,L]
RewriteRule ^ errors/404.html [L,NC]
Now the Problem is
If I I enter the URL like http://localhost/about in my web browser it well always shows the custom 404.html
I think the problem is on the last line.
If i remove the last line it will works.
But I also want the 404 page. Like If the user enters http://localhost/SomeRandomString the 404 page show on this url.
I also tried ErrorDocument on .htaccess file but it will change the url
Is any solution for this problem.
Have your htaccess rules file in following way. Place it in your root directory, make sure to clear your browser cache before checking your URLs.
RewriteEngine On
<IfModule mod_speling.c>
CheckSpelling On
</IfModule>
##Rules that cover your uris starting with course here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(course)/([^/.]+)/?$ $1.php?name=$2 [NC,QSA,L]
##Rules that cover your uris starting with about/contact/downloads here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(about|contact|downloads)/?$ $1.php [NC,QSA,L]
##Rules that cover all uris which aren't matching any of the above conditions.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ errors/404.html [L,NC,QSA]
I've been playing about with my .htaccess file and so far that's doesn't work.
I'm trying to force the .www prefix, while removing the .html extension and adding/force a trailing slash at the end of URL.
Example (with test.html file):
http://www.domain.com/test.html
http://www.domain.com/test
http://domain.com/test.html
http://domain.com/test
= http://www.domain.com/test/
My .htaccess:
Options +FollowSymLinks +MultiViews
RewriteEngine on
Rewritecond %{HTTP_HOST} ^laforgenumerique.fr$
Rewriterule ^(.*) http://www.laforgenumerique.fr/$1 [QSA,L,R=301]
#REMOVE DOT HTML
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
#FORCE TRAILING SLASH
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
#CUSTOM ERROR DOCUMENT
ErrorDocument 400 http://www.laforgenumerique.fr/404.html
ErrorDocument 401 http://www.laforgenumerique.fr/404.html
ErrorDocument 403 http://www.laforgenumerique.fr/404.html
ErrorDocument 404 http://www.laforgenumerique.fr/404.html
ErrorDocument 500 http://www.laforgenumerique.fr/404.html
Please help!
Host: OVH
You wrote:
#REMOVE DOT HTML
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
#FORCE TRAILING SLASH
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/]+$ %{REQUEST_URI}/ [L,R=301]
It seems that you are trying to mix two separate rules.
[L] suffix means that this is the last rule; all rules below will be skipped even if they are suitable.
You may try something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ www.$1
RewriteRule ^(.*).html$ $1/ [L]
I'm trying to have clean URLs, here is my code:
Options -Multiviews -Indexes +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteEngine ON
RewriteRule ^([0-9a-z\-\_]+)?$ profile.php?q=$1 [QSA,L,NC]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
ErrorDocument 404 /page/error?q=aqw
The only issue is that, i can not call a directory, i have to specify the file name, for exemple:
Help //this is not working
//This is working
So how can i fix that, thanks
Looking at your rules I think your first rule is redundant that you can remove.
Try this in your document root:
RewriteEngine On
RewriteBase /
ErrorDocument 404 /error?q=aqw
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
i've set the rules,
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ user.php?userid=$1
it redirects http://localhost/username to http://localhost/user.php?id=username works fine,
but for other existing folder page still redirects to user.php page; not that existing folder, ex http://localhost/folder it still redirects to http://localhost/folder/?userid=folder,
how can it make it to work for existing directories, ex when i type http://localhost/folder then page should shown of /folder/ directory ???
Replace your .htaccess code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^(messages)/?$ index.php?page=$1 [L,NC,QSA]
RewriteRule ^(A-Z0-9_-]+)/?$ user.php?userid=$1 [L,NC,QSA]
If it still doesn't work please post matching long entries from access.log and enable RewriteLog in your httpd.conf and post logs from there in your question.
The problem is that your second RewriteRule has no RewriteConds. Your two RewriteConds only apply for the first rule, then they stop. The second rule therefore applies unconditionally to every request URI.
So this part of your .htaccess should look like this instead:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^messages/?$ index.php?page=messages [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ user.php?userid=$1 [L,NC,QSA]