.htaccess hide extension while forcing www - apache

I have the following htaccess file, which hides the php extension and forces all URLs to use www:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
For the first part the credit goes to this answer and the second one to this answer.
This works perfect in most cases, however there is one problem. When a user tries to go to http://example.com/foo (or instead of "foo", whatever other page) it redirects to http://www.example.com/foo.php .
How can I make it redirect to http://www.example.com/foo , that is, whithout the .php?

You just need to change the order of your rules. First external redirect then the internal one. Have your code like this:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
I have tested it and works fine on my end.

What is "foo" in this case, a file? If so, this works:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you want to exclude "foo" specifically,
RewriteRule ^foo$ - [L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you're trying to use a web framework, then they probably want you to redirect all non-file and non-directory requests to something like index.php.
Edit: Try swapping and consolidating the two sections:
Options +FollowSymLinks
Options +Indexes
<IfModule rewrite_module>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>

Related

How can I use .htaccess to hide extension and prevent user access with extension url?

here is my code for hide .html and .php extension when user view my site, http://example.com/xxx.html to http://example.com/xxx
But I hope to make user when they input http://example.com/xxx.html, they cannnot access and jump to 404 not found.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# Remove .html-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)/$ $1.html
# End of Apache Rewrite Rules
</IfModule>
Anyone have solution?
You can add this as your first rule:
RewriteCond %{THE_REQUEST} \.(?:html|php)\s [NC]
RewriteRule ^ - [R=404,L]
Update
Perhaps this will fix your ErrorDocument problem:
RewriteCond %{THE_REQUEST} \.(?:html|php)\s [NC]
RewriteCond %{REQUEST_URI} !=/error404.html
RewriteRule ^ - [R=404,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 fails to redirect when encounter whitespace

I am using htaccess for clean url. Which is working fine as long as the parameters doesn't carry whitespace. In which case it converts the space in %20 and I receive 404 error.
So basically the URL: http://localhost:8080/series/dynamics/admin/cleanURL/green%20apple
gives me 404 error. But URL: http://localhost:8080/series/dynamics/admin/cleanURL/greenapple works fine.
Also is there a way I can remove the directory details from the URL, I tried
RewriteRule ^series/dynamics/admin/cleanURL/(.*)$ /$1 [L,NC,R]
But doesn't work
htaccess
<IfModule mod_rewrite.c>
Options +MultiViews
Rewriteengine on
RewriteBase /series/dynamics/admin/cleanURL/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^series/dynamics/admin/cleanURL/(.*)$ /$1 [L,NC,R]
Rewriterule ^([a-zA-Z0-9]+)/?$ index.php?product=$1
RewriteRule ^(.*)(\s|%20)(.*)$ /$3-$4 [R=301,L,NE]
#rewrite group and subgroup e.g. http://.../value1/value2/ [L,NC,QSA]
Rewriterule ^([a-zA-Z0-9]+(.*)+)/([^/]+(.*)+)/?$ index.php?product=$1&subgroup=$2 [L,NC,QSA]
</IfModule>
You will need to add this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) series/dynamics/admin/cleanURL/$1 [L]
Then have these rules in /series/dynamics/admin/cleanURL/.htaccess:
Options +MultiViews
Rewriteengine on
RewriteBase /series/dynamics/admin/cleanURL/
RewriteCond %{THE_REQUEST} /series/dynamics/admin/cleanURL/(\S*)\s [NC]
RewriteRule ^ /%1 [L,R=302,NE]
RewriteRule ^([^\s\x20]*)[\s\x20]+(.*)$ $1-$2 [L,R=302,NE]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
Rewriterule ^(\w+)/?$ index.php?product=$1 [L,QSA]
#rewrite group and subgroup e.g. http://.../value1/value2/ [L,NC,QSA]
Rewriterule ^([a-zA-Z0-9]+(.*))/([^/]+(.*))/?$ index.php?product=$1&subgroup=$2 [L,NC,QSA]

Change page url in htaccess

I have my own CMS system, but I can't make a code in .htaccess file.
I want redirect from http://example.com/?page=forum to http://example.com/forum...
I don't know, how do it...
I tried the following:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /projects/cms/
RewriteCond ?page=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
But it didn't work.
Sorry for my bad English, thanks for Code.
The comment splash58 posted is correct - you need to check the QUERY_STRING for the presence of page=<something>, like this:
RewriteCond %{QUERY_STRING} page=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]
Your file should now look like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /projects/cms/
RewriteCond %{QUERY_STRING} page=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
If it is still not working, then you need to ensure that mod_rewrite is enabled on your server, and that AllowOverride All is set in your server or virtual host configuration. See this for more information on how to do so: http://tildemark.com/enable-htaccess-on-apache/

.htaccess subdirectory redirect with non www to www and parameters

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /qsg/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?Item=$1 [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /qsg/
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?Item=$1 [L]
</IfModule>
I tried to look for an answer, but haven't been able to find one.
I am trying to redirect:
http://example.com/qsg/abcd
to
http://www.example.com/qsg/index.php?Item=abcd
I can get this working fine when the www is present in the URI. When I remove the www, it redirects to http://www.example.com/404.shtml
I've tried the two methods above with both the same result. I just can't figure out what I am doing wrong. I've seen plenty of other examples where this should work, but not for me. Do I have something wrong in my .htaccess file is there a possibility of something else causing the bad redirect?
Note: both the above rewrites are not included in the file. I've tried both independently.
Give this one a shot:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /qsg/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?Item=$1 [L,PT]
</IfModule>