htaccess to be used only in one folder - php

I've just started to use .htaccess and i'm having an issue here.
Inside my public_html folder i have these files.
index.php
profile.php
test.php
.htaccess
And when i go to profile.php file i have some parameters.
http://website.com/profile.php?id=1&name=Doe
For showing better SEO links i'm trying to make it appear like this
http://website.com/1/Doe
with this rewrite condition
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile.php?url=$1 [QSA,L]
</IfModule>
depended on this answer (click here)
and then in my php file i get the id of the user so i can make the queries etc with this code
$path_components = explode('/', $_GET['url']);
$ctrl=$path_components[0];
But the thing is that if i do so every file in my folder is trying to make the Rewrite Rule but i want a specific file... the profile.php one.
EDiT 1:
So from the comment below i made this htaccess file, and regardless the mod_rewrite is enabled on my server, the link is not changing at all.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/profile.php$ [NC]
RewriteRule ^([^/.]+)/([^/.]+)(?:/)?$ /profile.php?id=$1&name=$2 [L]
</IfModule>
And my php file now..
$path_components = explode('/', $_GET['id']);
$ctrl=$path_components[0];
But the link
http://website.com/profile.php?id=1&name=Doe
is not changing at all.

You need to redirect your old url to the new one, put this above your existing rule
RewriteEngine on
RewriteCond %{THE_REQUEST} /profile\.php\?id=([0-9]+)&name=(.+)\sHTTP [NC]
RewriteRule ^ /%1/%2? [L,R]
RewriteRule ^([^/]+)/([^/]+)/?$ /profile.php?id=$1&name=$2 [L]

Hello the issue is simple: you should exclude existing files/folders from rewriting process.
I would recommend to have only one index.php page where you get all the request and process in one place. Anyway your working htaccess file should look like this:
<IfModule mod_rewrite.c>
RewriteEngine On
# exclude real folders/files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^ index.php [L] - # redirect anything to index
# for your version
RewriteRule ^(.*)$ profile.php?url=$1 [QSA,L]
</IfModule>

Related

Removing 2 folders from URL and redirecting to page

I am creating a new web app and this is how my project folders look like:
So as you can see I have renamed that htaccess in the root folder because I am not using that one and I am using the one situated inside of the public folder. I am trying to change my URLs from this:
http://example.site/public/pages/about
to:
http://example.site/about
I was able to remove the .php from the file extensions but the code I added to remove the folders from the URL is not working.
Note: in the root folder (App/index.php) the index.php file simply has this:
<?php
header('Location: public/pages');
so I am just using this to send to the correct folder.
This is what I have so far:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /App/public/
RewriteRule ^pages/(.+)$ /$1 [L,R=302,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) $1.php [NC,L]
I have seen this question/answer from this url:
Remove / Rewrite 2 directory names from URL
but it is not working for me. Looking forward to anyone who can help, thanks.
Assuming ``htdocs/app/is yourDocumentRootforapp.blu` domain.
Inside htdocs/app/public/.htaccess you may use this code:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# if URL has /pages/ then remove and redirect
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^pages/(.+)$ /$1 [L,R=301,NC]
# landing page for app subdomain to show index.php
RewriteRule ^$ pages/index.php [L]
# check & rewrite if a matching .php file exists in pages/ subdirectory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/public/pages/$1.php -f
RewriteRule ^(.+?)/?$ pages/$1.php [L]
Besides this you will need to use this code in htdocs/app/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# send all the traffic to /public/
RewriteRule .* public/$0 [L]

Correct way to remove index.php in URLs

I'm currently learning fuel php and I've searched a lot in google all day long and still can't find a solution on how to remove index.php in URLs. That is, instead of
localhost/project/public/index.php/hello
I only need to write:
localhost/project/public/hello
I'm currently doing the tutorial here and when I clone their repository I'm getting a 404 error not found due to there URL having no index.php. How can I do that? I'm using WAMP server.
Create an .htaccess in the top level directory and add:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
</IfModule>
Try to add this .htaccess file to your web directory:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php [NC,L]

Wordpress .htaccess file with current system

Okay, so I'll explain this to the best of my abilities. I have Wordpress files running from a separate directory "_esSite" in the root folder. To do this, I must update my .htaccess file with some code from Wordpress. HOWEVER, the code in the .htaccess file from Wordpress is now not allowing my real directories open properly because it tries to run it through Wordpresse's index.php. Basically, if the folder/directory actually exist on the server, I want it to ignore the Wordpress code in the .htaccess file. Hope that makes sense.
Example case:
If I have a folder on my server named "/xyz/", then I want the htaccess file to basically read everything ABOVE "# BEGIN WordPress" in the htaccess file.
If I enter "/abc/" and the folder doesn't exist, I want it to read everything BELOW the "# BEGIN WordPress".
Please let me know if you have any questions and I'll try to clarify. Below is my current htaccess file. Thank you very much!
DirectorySlash Off
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
# add a trailing slash to directories
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ _client.php?var1=$1&var2=$2&var3=$3&var4=$4&var5=$5 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ _client.php?var1=$1&var2=$2&var3=$3&var4=$4 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ _client.php?var1=$1&var2=$2&var3=$3 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ _client.php?var1=$1&var2=$2 [L,QSA]
RewriteRule ^([\w-]+)/?$ _client.php?var1=$1 [L,QSA]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /_esSite/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /_esSite/index.php [L]
</IfModule>
# END WordPress

Codeigniter: Rewrite to remove index.php not working

I've just uploaded my website from my local server (XAMPP). It was working locally, but for some reason the rewrite to add index.php to my SEF URL's is not working in my public server. This is what I've got right now:
# Avoid listing directory
Options -Indexes
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
# manage language segment
RewriteRule ^(es|en)/(.*) $2?lang=$1 [L]
# code that allows to get rid of index.php from URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
</IfModule>
These URL's work:
www.example.com
www.example.com/index.php/aboutme
while URL's like this generate a 500 error:
www.example.com/aboutme
Here's another combination of condition and rule I tried to solve the index.php removal:
RewriteCond $1 !^(index.php|css|img|scripts|ckeditor|robots.txt|sitemap.xml)
RewriteRule ^(.*)$ index.php/$1 [L]
But it generates a 500 error for any URL without index.php, including the root URL www.example.com
Could you help me fix this?
There seems to be a typo in your RewriteRule.
Try this:
RewriteCond $1 !^(index.php|css|img|scripts|ckeditor|robots.txt|sitemap.xml)
RewriteRule ^(.*)$ /index.php/$1 [L]
It works for us with Code Igniter. The only difference is the slash infront of index.php when compared to your second examples.

mod_rewrite: Redirect to cache or script

I'm having some troubles with my mod_rewrite configuration, for a "cache" solution that I'm working on. I have directory called "cache" with compiled html files. I also have an index.php file which handles all the requests.
Right now all requests are rewritten to the index.php file like: "/articles" => index.php?q=articles.
But now I want it to rewrite to "cache/articles/index.html" if it exists, or else just fall back and rewrite to the index.php script.
I have messed around with the rewrite for a while know, but I can't get it to work. The rewrite code looks like this now (without anything for the cache):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
Thanks in advance :)
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#if the index.html exists in the cache
RewriteCond %{DOCUMENT_ROOT}/cache%{REQUEST_URI}/index.html -f [NC]
#then display it
RewriteRule ^ cache%{REQUEST_URI}/index.html [L]
#other existing rules go here

Categories