Remove sub folder url in htaccess - php

I have got it right to remove my subfolder from the url but its making my other root sub folders not pick up.
Eg...
www.blah.com/templates/index.html is now www.blah.com/index.php
but my www.blah.com/systemfiles/signin.php gives an error
I also have www.blah.com/systemfile/signin.php.
I can not access other sub folders.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/templates
RewriteRule ^(.*)$ templates/$1 [L]

The four lines you added says:
Follow symlinks
Start the enginge
If not /templates is in the URL
Treat the URL as /template was added
That is why it does not work for you. This strategy is kind of weird... But to make it work with your example you should check if the url points to a file or folder.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/templates
RewriteRule ^(.*)$ templates/$1 [L]

Related

Apache mod_rewrite ignore some real directories

That's my root directory:
css
img
js
includes
templates
api
and that's my .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?ur=$1 [L,QSA]
It works fine but when I inform the url a directory exists, the Apache open the directory, when in fact it should inform the URI for ur parameter.
For example:
mydomain.com/test-uri/ goes to ur parameter.
mydomain.com/api/ apache loads the api directory.
I did some research, but the most we got was to consider all directories when I need only pass the api directory as parameter.
Remove the -d check:
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?ur=$1 [L,QSA]

.htaccess PHP - Prevent rewriting when only domain is entered

I have a site located at
application
controllers
views
...
public <- the root of the site
index.php <- entry point
css
...
So the url is http://localhost/application/public. I would like when the user enters only http://localhost/application/public to do nothing except calling index.php without showing it.
Here are some examples:
http://localhost/application/public -> http://localhost/application/public or http://localhost/application/public/
BUT
http://localhost/application/public/asdf -> Rewrite internally to http://localhost/application/public/index.php and show only http://localhost/application/public/asdf
I have written the .htaccess. The only problem is that when entering http://localhost/application/public it opens http://localhost/application/public/index.php instead of http://localhost/application/public
One more thing - I want existing directories to be rewritten, so only existing files should not be rewritten to index.php So basically http://localhost/application/public/css should be rewritten to index.php. The reason I want this is to ommit the 403 Forbidden error, and kindly tell the user that the path does not exist.
HTACCESS
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /application/public/
#Remove trailing slash
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=301,L]
#Do the rewriting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [NC,L]
The reason I put RewriteCond %{REQUEST_FILENAME} !-d is to prevent http://localhost/application/public from rewriting. If http://localhost/application/public is rewritten, it loads http://localhost/application/public/index.php
Actually I know there are needed some modifications on RewriteCond %{REQUEST_FILENAME} !-d. I want the root, public folder to not get rewritten, it should be the only exception. All of the rest physical folders like css, js, have to be rewritten, like not existing. Files should not be rewritten. Thanks!
Ok, I think I found a solution.
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /application/public/
RewriteRule ^(css/?|js/?)$ index.php [NC,L] <- Folders css, js will be rewritten, but root directory won't be rewritten. Files inside them also won't be rewritten.
#Remove trailing slash
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=301,L]
#Do the rewriting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [NC,L]

Rewriting all request to index.php in the same diectory

I want all the request to be rewrited to index.php so I used this htaccess code.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
It works fine when site is hosted on home directory. But suppose if I move my site to a subdirectory xyz and move all my files including .htacess file inside this directory. Then if I access http://example.com/xyz/some_page, the request is not redirected to /xyz/index.php.
So, How can I make this rewrite work even on subdirectories as in my case.
Update:
I forgot to mention the directory xyz as in my case is likely to change frequently. So, this directory doesn't need to be hard coded in the rewrite rule
Remove the RewriteBase
If you don't need the rewrite base - just remove it. A working example based on the one shown in the question would be:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Note that the first rewrite rule has been deleted as it is logically equivalent to RewriteCond %{REQUEST_FILENAME} !-f (do not rewrite requests for a file that exists).
Consider also deleting RewriteCond %{REQUEST_FILENAME} !-d as this prevents urls like /this/folder/exists being sent to index.php. Most relevant if Directory listings are enabled but not desired.
change your base url for rewrite
RewriteBase subdir/
or try with full url
RewriteBase http://www.examle.com/subdir/

How to use mod_rewrite and edit URL path?

I apologize to ask such a question, there's hundreds of them all around, and I wouldn't ask if I didn't need to understand. I've tried countless times on .htaccess but unable to make it work, I'm also testing on local server but I'm sure mod_rewrite is on, so should be errors I'm making.
I'm currently trying to change m URL from
example.com/article.php?article_id=article-id-goes-here&article_title=article-title-goes-here
to
example.com/article/75/article-title-goes-here
The latest code I've tried was the following:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ webroot/article.php?article_id=$1&article_title=$2 [QSA,L]
My folder structure currently stands as
blog/
libraries/
articleClass
articleClass.php
webroot/
css/
images/
js/
.htaccess
I forward everything to webroot as that's my index page.
You have an extra slash at the end of your pattern that isn't present in the URL that you say you are using:
RewriteRule ^([^/]+)/([^/]+)/?$ webroot/article.php?article_id=$1&article_title=$2 [QSA,L]
You can make is optional by adding a ? after it.
example.com/article.php?article_id=article-id-goes-here&article_title=article-title-goes-here
to
example.com/article/75/article-title-goes-here
Try this rule:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/([^/]+)/([^/]+)/?$ webroot/article.php?article_id=$1&article_title=$2 [QSA,L,NC]

.htaccess all request to other file

Currently I have the following .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php/$1 [L]
</IfModule>
Which works allmost perfect.
It rewerites urls like http://domain.com/something/ to the public/index.php file, like a charm, except when it is a file, just like it should.
However http://domain.com (without any path appended) (there is no index.php in the root, so it gives a 404 at the moment) is not being rewrited, how can I change this .htaccess so it rewrites this url too?
The index file is in public/index.php I want it to load that file through the use of .htaccess
Thanks
I believe to rewrite the root, you can simply do something along the lines of:
RewriteRule ^$ location/of/root/file [L]
You could try:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php
RewriteBase should prepend the rule pattern with a leading slash, forcing it to match the root path.
Untested!
What you have there is inspired by WordPress?? It's a bad idea as it tell Apache to always check if the path is a file or a directory before redirecting.
I have something like this
RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*/(css|images|javascript)(.*) [NC]
RewriteCond %{REQUEST_URI} !\.(swf|ico|php|xml)$ [NC]
RewriteCond %{REQUEST_URI} !robots.txt
RewriteRule (.*) index.php?page=$1&%{QUERY_STRING} [PT]
The first condition restricts this redirect from working in specific folders.
The seconds does it for specific extensions.
You can guess what the third does :)

Categories