clean URLs with htaccess - apache

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]

Related

Renaming file in URL (.php file)

I have a file called filename-step1.php and looking to make the url domain.com/filename/step1 through .htaccess.
My current .htaccess file looks like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
At the moment, this works fine for removing the .php tag from the url. Just need to fix the directories.
You could add a RewriteRule to redirect to $1/$2.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /$1-$2.php [NC,L]
</IfModule>
So, filename/step1 will trigger the filename-step1.php file.
I am giving 2 solutions here, please try one at a time only in your .htaccess file. With your shown samples, could you please try following as a Generic rules.
RewriteEngine ON
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ $1-$2.php [NC,L]
OR in case you are looking for specifically url filename/step1 try following.
RewriteEngine ON
RewriteCond %{HTTP_HOST} domain\.com [NC]
RewriteRule ^(filename)/(step1)/?$ $1-$2.php [NC,L]

Dealing with trailing slashes in htaccess

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]

How to get htaccess to load page without file extension

I have searched around and tried a few different things, with no luck.
I am trying to load this page http://SERVERNAME.com/get.php when I use the url http://SERVERNAME.com/get
Here is what I have in my htaccess now:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
I have tested to make sure my htaccess file is being read by intentionally breaking it and getting a 500 error.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{THE_REQUEST} ^(?:GET|POST)\ /.*\.php\ HTTP.*$ [NC]
RewriteRule ^(.*)\.php$ $1 [R=301,L]

.htaccess: Remove extension and force traling slash (seo friendly)

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]

.htaccess RewriteRule trouble

I am guessing this question has been asked many times, but i could not find one that would perhaps give me what I need.
So.. I can access the scripts by the following url's:
http://website.com/index.php/hello/world
http://website.com/hello/world
Both go to index.php which parses the input (hello/world in this example).
this is my .htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?path=$1&%{QUERY_STRING} [L]
</IfModule>
However. When the site is accessed like this:
http://website.com/index.php/hello/world
the RewriteRule outputs something similar to index.php?path=index.php/hello/world
I want to remove that index.php after path= in the RewriteRule
Your .htaccess file should look like this (notice the new rule that checks if index.php is a part of url):
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.php/(.*)$ index.php?path=$1&%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?path=$1&%{QUERY_STRING} [L]
</IfModule>
Try this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Modifying answer from Stackoverflow answer to suit this question
The .htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Redirect user
RewriteCond %{THE_REQUEST} ^.*index.php.*
RewriteRule ^(.*)index.php(.*)$ $1$2 [NC,R=301,L]
# Handle the query to PHP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?path=$1&%{QUERY_STRING} [L]
</IfModule>