Dynamically Parsing a URL in .htaccess - php

Give the following URL:
http://www.example.com/file_to_load/slug1/slug2/slug_n
I would like my .htaccess to load file_to_load.php (or whichever filename takes at position in the URL)
In addition, file_to_load.php is located one directory down from .htaccess
.htaccess
|_subdirectory
|_file_to_load.php
My current .htaccess is as follows (redirecting all requests to index.php):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?params=$1 [NC]
How can I rewrite my .htaccess to perform the above?

You can to like this in subdirectory :
RewriteEngine On
RewriteRule ^(.*)$ file_to_load.php?slug=$1 [L]
Hope this helps !

Related

.htaccess Hide php param keys from url but dont change physical path

I am looking for a solution to hide param keys from my url
for example /page1.php?city=Lahore
I want it to rewrite as /page1/Lahore
but the most important thing is Lahore is not a directory exist on server I want it to point to same file page1.php just rewrite url externally
thanks
create an .htaccess file from the root and paste this code
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^page1/(.*)? /page1.php?city=$1 [L]
or you could also put it in your website apache conf file.
try this code
for .htaccess file
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /projectfoldername/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^page1/(.+?)/?$ /projectfoldername/page1.php?key=$1 [L,QSA]
page1.php code will be
<?php
echo $_REQUEST['key'];
?>
then call
http://localhost/projectfoldername/page1/1947
output will be :1947

Codeigniter .htaccess redirect and hide URL part

I wish to have two Codeigniter installations one in my default root root/ and one in root/en. Problem is in my .htaccess file and in the location of default controller. Everything works in my default install, I have the default/front controller in the following directoy: root/public/index.php and my .htaccess looks like this in order to redirect and remove "public" from the url:
RewriteEngine on
RewriteBase /
ReWriteCond %{REQUEST_URI} !public/
ReWriteRule ^(.*)$ public/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]
Everything is fine in root, server automaticly redirects to root/public/index.php folder and reads "index.php" there, .htaccess then hides the "public/index.php" part of the url.
How can I write similar .htaccess file, so that when the user types the url "sitename/en" it automaticly redirects to root/en/public/index.php and hides the public/index.php part?
try using any of the following :
RewriteEngine on
RewriteBase /demo/
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
make sure config.php file has
$config['base_url'] = "http://".$_SERVER['HTTP_HOST']."/projectname";
$config['uri_protocol'] = 'PATH_INFO';

htaccess RewriteRule to shorten the URLs

I would like to find a RewriteRule that does this :
mysite.net/jqMAS/ or mysite.net/jqMAS => mysite.net/index.php?id=jqMAS
I used such a .htaccess file :
RewriteEngine On
RewriteRule ^(.*) index.php?id=$1
But unfortunately, it doesn't work (maybe mysite.net/index.php is itself redirected to mysite.net/index.php?index.php, etc. ?) : calling mysite.net/jqMAS produces a 500 Internal Server Error.
What RewriteRule should we use to do such URL shortening ?
Here is what the index.php page (I didn't mention the headers) looks like :
<body>
Bonjour <?php echo $_GET['id']; ?>
</body>
Try the following your .htaccess file, as it is the setup successfully used on my own website.
# Enable the rewriting engine.
RewriteEngine On
# Change requests for a shorter URL
# Requires one or more characters to follow the domain name to be redirected
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?id=$1 [L]
There are 2 htaccess files:
Keep your htaccess file within application folder as:
Deny from all.
and paste following code to your htaccess file outside the application folder:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
It is working perfect for me.
You need RewriteCond to stop rewriting for real files and directories:
RewriteEngine On
# if request is not for a directory
RewriteCond %{REQUEST_FILENAME} !-d
# if request is not for a file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?id=$1 [L,QSA]

Issue with htaccess

I made a PHP framework that has a built in TPL system. It fetches the ending of the url, and finds the file that has that string. So if the url is http://website.com/tagHere the Tpl system would look for the file called tagHere.php. Everything works fine with it, but I'm now trying to expand the framework to have a built-in backend panel. I've tried multiple ways, but it just refuses to work.
I decided to take a look at Wordpress' htaccess, since they do exactly what I'm trying to accomplish. Here is how their htaccess works.
They check if the requested file name is a file. If it isn't a file, then they check if it is a folder. If it isn't a folder, they redirect to index.php. I want it to check if it's a file/folder, and if it isn't to remove the .php tag from the URL, so the TPL system can get the specified file and return it.
Here was my original .htaccess file.
RewriteRule ^(|/)$ index.php?url=$1
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ index.php?url=$1`
Here is Wordpress' original .htaccess file.
<ifmodule mod_rewrite.c="">
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</ifmodule>
Here was my attempt:
<ifmodule mod_rewrite.c="">
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^(|/)$ index.php?url=$1
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ index.php?url=$1
</ifmodule>
When I try the above htaccess, it returns a 404 error.
The Rule
RewriteRule . /index.php [L]
Will always match every URL because it says something like "if there is a character in the URL, then redirect".
What you're looking for is something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin/(.*?)$ admin.php?location=$1 [L]
RewriteRule ^(|/)$ index.php?url=$1
RewriteRule ^([a-zA-Z0-9_-]+)(|/)$ index.php?url=$1

.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