I'm trying to simply the access to my API with a .htaccess file and I'm facing an issue.
For example, what I'd like to achieve is:
User reaches http://localhost/teammngr/user/photos/secrethash and is automatically redirected to http://localhost/teammngr/user/photos.php?sid=secrethash.
User reaches http://localhost/teammngr/user/config/secrethash and is automatically redirected to http://localhost/teammngr/user/config.php?sid=secrethash.
User reaches http://localhost/teammngr/team/members/secrethash and is automatically redirected to http://localhost/teammngr/team/members.php?sid=secrethash.
If the user wants to reach these files directly, it is supposed to be possible.
Moreover, the url http://localhost/teammngr/ will be under a subdomain like http://team.mywebsite.com/.
So far, I've made the following .htaccess file, but it keeps throwing a 500 error on my server.
To be clear, this file is not in the root directory but in a sub dir.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /teammngr
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/([a-z_]+)/([A-Za-z0-9_-]+)$ /user/$1.php?sid=$2 [L]
RewriteRule ^team/([a-z_]+)/([A-Za-z0-9_-]+)$ /team/$1.php?sid=$2 [L]
Where did I make a mistake ?
Thanks for your precious help.
RewriteCond is only applicable to very next RewriteRule. To avoid repeated RewriteCond before every rule you can have a separate rule to ignore requests for files and directories. Also remove / from your target URIs.
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /teammngr/
# ignore requests for files and directories from rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^user/(\w+)/([\w-]+)/?$ user/$1.php?sid=$2 [L,QSA]
RewriteRule ^team/(\w+)/([\w-]+)/?$ team/$1.php?sid=$2 [L,QSA]
you can try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /teammngr
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/([a-z_]+)/([A-Za-z0-9_-]+)$ user/$1.php?sid=$2 [L]
RewriteRule ^team/([a-z_]+)/([A-Za-z0-9_-]+)$ team/$1.php?sid=$2 [L]
Related
Good day,
I am using a framework that uses a .htaccess file to forward all requests to a sub file core/index.php
I used the following .htaccess file code :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ core/ [L]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) core/$1 [L]
</IfModule>
This worked great I thought until I noticed that the pages got loaded multiple times.
And this is what I want to prevent.
So I had a little look at this forum.
And I found a solution that the space between (.*) and core could be the reason.
Hence I removed it.
And to my delight the multiple db inserts stopped.
However now I have found that my requests do not get forwarded correctly.
Only the main request (www.myapplication.com/) gets forwarded correctly (index.php)
Whenever I am adding something like : (www.myapplication.com/admin) he bugs out and cannot find the page.
I hope anyone could tell me what to modify in this code :
RewriteEngine on
RewriteRule ^$ core/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) core/$1 [L]
Thanks in advance!!
I also note that whenever I change the (.) core/$1 [L] line to (.)core/$1 [L] that not only my (sub)pages stopped working but for the index.php the load times of my index.php file where split in half.
Hence my page truly gets reloaded multiple times.
Edit
As pointed out by a question below the system also uses a .htaccess file inside the /core directory.
This file has the following contents
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
Edit
I think it would come handy to make a small summary. If requested for a file or path I want the server to check if it is
available on the server.
www.myapplication.com/flower.jpeg and the server can find it it should display the flower.jpeg file.
If the server CAN NOT find the flower.jpeg file I want the server to forward the request to /core/index.php?url=flower.jpeg
I am using two .htaccess files
in the **public directory** (main directory)
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ core/ [L]
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) core/$1 [L]
RewriteCond %{REQUEST_URI} !core/
</IfModule>
Second file in the core directory
core/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
The issue: my pages are in a loop hence the page index.php get's executed with every server request. E.G. 10 images on a page means 10
requests extra. Same for js and CSS files.
edit:
Solved!!!
This is the solution!!
DirectoryIndex core/index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ core/index.php?url=$1 [QSA,L]
</IfModule>
You have to add
RewriteCond %{REQUEST_URI} !core/
to the RewriteCond block to prevent redirection from the target to the target.
It would be better if your target would be more specific, for example if you could specify /core/ instead of core/ (do you have a core subfolder in every folder?). Also, you will have to exclude image folders.
Try this in /core/.htaccess:
ErrorDocument 404 default
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)/?$ index.php?url=$1 [QSA,L]
I found a solution. The working code is :
DirectoryIndex core/index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ core/index.php?url=$1 [QSA,L]
</IfModule>
I am fiddling around building my own CMS and playing around with the .htaccess file.
I was looking around the web for a snippet of code which would add a trailing slash onto every HTTP request.
When I inserted the code I realised that it would also remove the name of the subdirectory in which I was working.
URLS:
So this is what I had: localhost/project/index.php
This is what I wanted: localhost/projects/index.php/
This is the result I got: localhost/index.php/
But the real problem is this:
I saw the outcome, and I wanted to revert to example 1 from example 3. But after I deleted the code in the .htaccess file, the changes remained, and I have no idea how to revert this.
How do I revert this rewrite rule?
This is my .htaccess before implementing the "trailing slash"
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /tesla0.2/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
This is my .htaccess after:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /tesla0.2/
RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
I assume that youre htaccess file is in the "projects" directory?
Try changing your rule to:
RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
You'll also need to clear your browser's cache, since the redirect is 301 (permanent), the browser will cache the redirect and use it indefinitely, since it's a permanent redirect.
Goal: Send all requests, regardless whether the directory/file exists or not, to http://example.com/index.php
What I have tried:
Rewrite Engine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://example.com/index.php [L]
It redirects URL's of non-existing directories/files fine, but when I access an existing subdirectory and file http://example.com/test/test.php it only gives me the contents of the test.php page instead of the main index.php page.
If I understand correctly, the RewriteCond %{REQUEST_FILENAME} !-f and RewriteCond %{REQUEST_FILENAME} !-d tell it to perform the RewriteRule if those criterias are met, which makes sense why it only redirects on non-existing directories and files. But I've tried removing those RewriteCond's, and all it does is put my site into a redirect loop.
I've also tried doing
Rewrite Engine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ http://example.com/index.php [L]
But then it doesn't redirect at all.
What do I have to put to accomplish this?
Your first example would only redirect things that does not exist to your index.php as the conditions you have there:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Basically means if file or directory does not exist, do this.
Your second example is even worst as it tries to see if a file, directory exist and does not exist which makes no sense.
To redirect anything existent or not to a main handler in this case index.php all you need is the below:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index.php$ [NC]
RewriteRule ^ index.php [L]
The condition makes sure you're not redirecting it to yourself to prevent a loop. If you're running HTTPD version 2.4 and above you can simple use:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^ index.php [END]
The flag [END] takes care of not executing any further redirects.
My question might be dumb, but I googled it and didn't find an answer...
Let's say I want to access my website in this given url: www.mywebsite.com/something/something_else/?some_query_string=true
(I put the query string because it is going to be there, and I don't know if it makes any difference in the htaccess file)
I want it to keep the URL the same, but load the index file for no matter what URL, which is not in the root of the server.
My server has an "application" folder, where all the code is.
How can I do this?
Thanks!
use htaccess to re-write all request to index.php (except for when a file/dir/link is requested and it exists):
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
If you want to use Rewrite to have your requests handled by a file outside the DocumentRoot, then you can combine with an Alias directive.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* application/index.php [PT]
Alias application /path/to/application
Note the [PT] on the RewriteRule which means 'pass through' - it ensures the rewritten url is passed through to other apache modules which might be interesting in processing it.
This turned out to answer my own question:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ application/index.php?url=$1 [QSA,L]
If the URL doesn't exist, it loads the index.php file inside of the folder "application", and it keeps the URL the same, which was exactly what I needed...
Thanks for the answers!
I'm running in to a mod_rewrite issue where the second rule in my .htaccess file is overriding the first. The .htaccess file in question looks like the one below:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /path/appname
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /api/v1/(.*)$ api/v1/index.php?rquest=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
</IfModule>
The issue that I'm seeing is this:
If I go directly to http://example.com/path/appname/api/v1/valid/endpoint the first RewriteRule triggers correctly and I get the result back from the API.
However, say I visit http://example.com/path/appname/app - a page which has been rewritten according to the second RewriteRule. This page makes AJAX requests to the api/v1 page. Those requests are instead directed through the second RewriteRule and send to my base index.php page.
I'm confused on how this could be, as my understanding is that the [L] flag prevents any further rules from being run once it matches and thus once any request that has 'api/v1' in it should catch that and stop checking for any further matches. What do I need to change in order for this to work correctly?
Thanks!
You should exclude the segment path for the previous rule-set, so it is not processed again. Like this:
# Don't redirect/map when folders or files exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Exclude the previous path
RewriteCond %{REQUEST_URI} !api/v1/? [NC]
# Prevent loops
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule . index.php [L]
</IfModule>
Replace the last 4 lines in you question with the above lines.