In my site when I go to URL such as my-site.com/css or my-site.com/js, then browser displays the folder listing instead of the actual page saved in the database. If I use Options All -Indexes in my htaccess then it says forbidden. I have following rule in my htaccess to load any dynamic page:
RewriteRule ^(.*)$ page.php?url=$1 [QSA,L]
Since I have also folders named css and js in my project, so I think server displays the directory content instead of dynamic page. How can I prevent this conflict. I know if I use a suffix "page" before my page name then it can be sorted out but i don't want to use any parameter before the page url.
EDIT
Here's my complete htaccess file:
# my-site.com
Options +FollowSymlinks -MultiViews
#RewriteBase /
#Enable mod rewrite
RewriteEngine On
DirectorySlash off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ page.php?category=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/([^/]+)$ page.php?category=$1&post=$2 [QSA,L]
Since /js and /css are real directories your rewrite rules don't execute because you have this condition:
RewriteCond %{REQUEST_FILENAME} !-d
which means don't run for directories.
You can tweak your .htaccess to this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !/$ %{REQUEST_URI}/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ page.php?category=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ page.php?category=$1&post=$2 [QSA,L]
Related
I'm running a web app in azure and I can only use .htaccess to achieve the behaviour I'm looking for: if the requested URL doesn't match a file under /app or /dist folder then redirect the request to /dist/main.php. This works well if I try something like: http://example.com/ (the request goes into /dist/main.php without any URL redirect) but if I instead use http://www.example.com then it returns a 301 to http://example.com/dist/main.php and I have no idea why?
.htaccess content under wwwroot:
<IfModule mod_rewrite.c>
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/dist%{REQUEST_URI} -f
RewriteRule .* dist/%{REQUEST_URI} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/app%{REQUEST_URI} -f
RewriteRule .* app/%{REQUEST_URI} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /dist/main.php [L,QSA]
</IfModule>
I also tried the last line as: RewriteRule ^ /dist/main.php [L,QSA]
I made Ajax navigation that call pages from a /pages file in a div, and I've tried many things to remove the .php from my URLs but I can't find the solution.
Here is my .htaccess file:
Options +MultiViews +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9\-]+)\.php$ index.php?p=$1 [L]
RewriteRule ^pages/([a-z0-9\-]+)$ index.php?slug=$1
So everything works well but I can't change my URL: www.mywebsite.com/about.php to www.mywebsite.com/about
Try using this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
It should remove .php extension from the end of your URLs.
i want to change my website urls exemple www.mysite.com/about.php to www.mysite.com/about/
i'm using this code in my htaccess file
# Apache Rewrite Rules
<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
# End of Apache Rewrite Rules
</IfModule>
i tried this one too
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
it works but all the page content becomes inside the /about/ its like i created a folder that does not exist so all the internal links are changed , if i have a link to post.php it goes to /about/post.php
all the included pages are not working too such as css and images
sorry my english isn't so good .
i finally found what i wanted ,i used this meta tag in the header section
`<base href="http://localhost/mysite/" />`
now everything works
ps: don't use localehost on webhost
I'm cleaning my URLs and everything looks fine but whenever I try to access any directory such as images etc then chrome shows that "This webpage has a redirect loop." However I want to protect directories inside public_html.
The .htaccess file is inside public_html
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
</ifModule>
I want to protect images, css and javascript directories but want to allow access to the admin directory.
Thank in advance.
Change order of your rules and change your trailing slash removing rule:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=301,L]
# block all directories except admin/
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !^admin(/|$) - [NC,F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</ifModule>
You can use this remove all code and use this one
option index allow and disallow to be index directory like
This does the trick.
Options All -Indexes
or
IndexIgnore *
for more information please check below link
http://viralpatel.net/blogs/htaccess-directory-listing-enable-disable-allow-deny-prevent-htaccess-directory-listing/
I have WAMP installed, and a project at in E:/wamp/www/project/index.php Links from index.php redirect to show.php I am using the below .htaccess script to be able to make URL in show.php appear as /article-title instead of what it normally does i.e show.php?id=xxx
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (.*)$ show.php?id=$1 [QSA,L]
But, the above .htaccess file which is found in project/ along side the index/show page does not seem to work. It does not even show me the project folder, when I go to www/
I don't know where the problem is, and rewrite-module is on
Given that your article URL is like this:
http://localhost/project/1122-some-title-comes-here
You can use this rule:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^project/(\d+)-([\w-]+)/?$ /project/show.php?id=$1 [QSA,L]
If you want your articles from the root folder like this:
http://localhost/1122-some-title-comes-here
You can change the above to:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(\d+)-([\w-]+)/?$ /project/show.php?id=$1 [QSA,L]
The above conditions means, if folder, files and symbolic links are false, rewrite it.
The rule itself ^(\d+)-([\w-]+)/?$ if it start with digits and a dash and contains additional alphanumeric characters and dash and end or not with / internally redirect to:
/project/show.php?id=$1
The $1 means to pass the result of the parenthesis from the rule to the id which is (\d+) which in our URL example would mean the number 1122.
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
"The RewriteBase directive specifies the URL prefix to be used for per-directory (htaccess) RewriteRule directives that substitute a relative path."