I want to hide my file extensions in browser to be displayed, like http://www.example.com/dir/abc.php should be displayed as http://www.example.com/dir/abc only.
i wrote following rewriteCond and rewrite rule on .htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
but this code doeesn't seems to helping me.
EDIT:
this is complete code of my htaccess file
Options -Indexes
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?127.0.0.1 [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?127.0.0.1.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost.*$ [NC]
RewriteRule \.(js|css|jpg|png)$ - [F]
RewriteEngine on
RewriteRule ^(.*)\.[\d]{10}\.(css|js|png|jpg)$ $1.$2 [L]
RewriteEngine On
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
COMPLETE .htaccess:
Options +FollowSymLinks -MultiViews
ErrorDocument 403 /errors/403.php
ErrorDocument 404 /errors/404.php
RewriteEngine on
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
# block direct hot linking
RewriteCond %{HTTP_REFERER} !^http://(www\.)?127.0.0.1 [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?localhost [NC]
RewriteRule \.(js|css|jpg|png)$ - [F]
RewriteRule ^(.*)\.[\d]{10}\.(css|js|png|jpg)$ $1.$2 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
I'm not an Apache expert by any stretch, but I'm curious why no one is recommending the negotiation module (mod_negotiation). Are there best-practice reasons to avoid using
I heard about it in passing at http://www.webhostingtalk.com/showthread.php?t=317269. After enabling mod_negotiation in my global configuration:
LoadModule negotiation_module /usr/lib/apache2/modules/mod_negotiation.so
I just had to enable it in my virtualhost's Options. Here's the whole config block:
DocumentRoot /var/www/site_directory/public_html
ServerName your.domain.here.com
<Directory /var/www/site_directory/public_html>
allow from all
Options +Indexes Multiviews
</Directory>
easy method is:
you create your link like
'Home' instead of 'Home'
Then your .htaccess should be like this:
RewriteCond %(REQUEST_FILENAME) !-d
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-l
RewriteCond ^home home.php [L]
The simplest and easiest way:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
Related
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]
I have following .htaccess rules which removing .php extension in urls from domain.com/index.php to domain.com/index/ and working fine
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^ %1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
Now I want to use httpd.conf file instead of .htaccess, but when I try rewrite it to:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^ %1/ [R=301,L]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}.php -f
RewriteRule ^/(.*?)/?$ $1.php [NC,L]
url is rewritten correctly to domain.com/index/, but server returns error 404.
I'll be glad for any help and suggestions, what could be wrong.
%{REQUEST_FILENAME} is already full filesystem path if file or directory is found so prefixing that with %{DOCUMENT_ROOT} will make an invalid path.
You can use these rules in httpd.conf:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteCond %{REQUEST_METHOD} !POST
RewriteRule ^ %1/ [R=301,NE,L]
RewriteCond %{DOCUMENT_ROOT}/$1 !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^/(.+?)/?$ /$1.php [L]
So I have the following code:
ServerName test.com
ServerAlias *.test.com
DocumentRoot /var/www/test.com/public_html
Options All -Indexes
FileETag none
ServerSignature Off
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} .+
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^(.*)\.test\.com$ [NC]
RewriteRule ^$ /%1.php [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^([^.]+)\.test\.com$ [NC]
RewriteRule ^(.+)/?$ /%1.php?o=$1 [L,NC,QSA]
</IfModule>
On my old server I had this in a .htaccess file (minus the documentroot part) and it worked fine doing this:
sub.test.com -> test.com/sub.php
It still does this correctly, but now that I have moved it into the sites-available config file, if I try to access a file, it breaks and uses that same rule as the file to get:
sub.test.com/file.css -> test.com/sub.php
Is there a way to keep it from doing this? (I tried doing RewriteCond %{REQUEST_FILENAME} !-f and such but it didn't seem to care much about that rule).
Thanks
Inside Apache config try this code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} .+
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^(.*)\.test\.com$ [NC]
RewriteRule ^/?$ /%1.php [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^([^.]+)\.test\.com$ [NC]
RewriteRule ^/?(.+?)/?$ /%1.php?o=$1 [L,NC,QSA]
</IfModule>
Apache config matches a leading slash in RewriteRule unlike htaccess
I have the following in my htaccess:
Options +FollowSymLinks -Multiviews -Indexes
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
Now, I am trying to add some pagination rules which left my htaccess file as follows:
Options +FollowSymLinks -Multiviews -Indexes
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^beta/network/showcase/category/(.*)/page/(.*)$ beta/network/showcase.php?category=$1&page=$2 [L]
RewriteRule ^beta/network/showcase/category/(.*)$ beta/network/showcase.php?category=$1 [L]
RewriteRule ^beta/network/showcase/page/(.*)$ beta/network/showcase.php?page=$1 [L]
RewriteRule ^beta/network/showcase/([0-9]+)$ beta/network/showcase_single.php?id=$1 [L]
They all work unless from this one:
RewriteRule ^beta/network/showcase/category/(.*)/page/(.*)$ beta/network/showcase.php?category=$1&page=$2 [L]
Which throws a 404 error as page not found:
The requested URL /beta/network/showcase/arts-entertainment/page/2 was not found on this server.
Why is that particular rule failing?
Thanks!
Issue was resolved per #anubhava response, I overlooked the '/category/' in the URL.
I already have website www.example.com which resides in /public_html/site.
Now I want to point www.example.com/anothersite to /public_html/anothersite.
How can I?
The .htaccess in /public_html:
RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !site/
RewriteRule (.*) /site/$1 [L]
The .htaccess in /public_html/site:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /site/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /site/index.php [L]
</IfModule>
# END WordPress
In The .htaccess in /public_html I have tried this but it says error 404 Page not found!:
RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^anothersite/(.*)$ http://example.com/anothersite/$1 [R=301,NC,L]
RewriteCond %{REQUEST_URI} !site/
RewriteRule (.*) /site/$1 [L]
But I dont know why it was not working last but now its working with the above code...
The .htaccess in /public_html:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule (?!^(?:site|anothersite)(?:/.*|)$)^.*$ /site%{REQUEST_URI} [L]