Rewrite all URLs to index with path as GET variable - php

I am trying to forward all requests for other index.php files to the root index.php file with the path encoded as a GET variable. Here are some examples of what I mean:
http://my-site.com/this/is/the/path/index.php would become:
http://my-site.com/?path=%2Fthis%2Fis%2Fthe%2Fpath%2Findex.php
http://my-site.com/this/is/the/path/ would become:
http://my-site.com/?path=%2Fthis%2Fis%2Fthe%2Fpath%2F
http://my-site.com/this/is/the/path/index.php?v=var would become:
http://my-site.com/?path=%2Fthis%2Fis%2Fthe%2Fpath%2Findex.php%3Fv%3Dvar
How can this be done with the .htaccess file?
EDIT
To be clear, here is what I am hoping to achieve:
If a path is to a file called "index.php" it redirects as long is it is not the root index file. Note, said index.php file could have GET variables that should be included.
If a path ends in a slash, in should redirect as well as long is it is not the root. Again, note said path could have GET variables that should be included.
Otherwise, it should not redirect any other paths.
In summary, only paths to an index.php file (that are not the root) or that end in a slash should be redirected.

put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?path=$1 [L,QSA]

You can use this .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?path=$1 [L]
</IfModule>

I suggest learning a little about regular expressions in Apache's mod_rewrite.
RewriteEngine On
RewriteRule ^(.*/index.php)$ index.php?path=$1
Note this will only rewrite URL's that end in index.php and leave all other URL's alone, as that's the behavior you specified. If you want to rewrite all URL's to the index:
RewriteEngine On
RewriteRule ^(.*)$ index.php?path=$1

Related

How to use RewriteRule in htaccess using XAMPP

How do I get rid of the folder name using .htaccess and XAMPP.
I saved the folder in --> xampp/htdocs/myproject
In myproject folder there are an index.php and .htaccess files.
RewriteEngine On
RewriteBase /myproject/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
index.php
<h1>index</h1>
<?php
var_dump($_SERVER["REQUEST_URI"]);
This redirects me to the index file but when i var_dump requested uri i'm getting also myfoler name.
example:
http://localhost/myproject/contact
i'm getting -->/myproject/contact
Is this possible to get rid of this foldername/path using .htaccess ?
In this case the /myproject/.
RewriteRule ^myproject/(.*)$ $1
This will take any url of the pattern "myproject/whatever" and rewrite it as "whatever". The string (.*) tells .htaccess to take everything after "myproject/" and store it in a backreference, which is referred to in the rewrite portion of the rule as $1. Hope this helps.

.htaccess Rewrite with Original Request Appended to Query String

I have a directory in my site with a bunch of temporary PHP generated PDF files.
mysite.com/crm/pdfs/
I have an .htaccess file located in the pdfs/ directory with the intention of redirecting all requests within that folder to the index.php page within that folder and include the original request in the query string. For example,
When a browser is pointed to:
mysite.com/crm/pdfs/somepdf.pdf
it should be redirected to
mysite.com/crm/pdfs/index.php?p=somepdf.pdf
Here's my current try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]
The problem with this is, when the user requests:
mysite.com/crm/pdfs/somepdf.pdf
They are instead redirected to
mysite.com/index.php
and the original request is not appended.
How can I achieve the desired result in my .htaccess that is located in the /pdfs directory?
Use this rule in your /pdfs/.htaccess:
RewriteEngine On
RewriteBase /crm/pdfs/
RewriteRule ^(.+?\.pdf)$ index.php?p=$1 [L,QSA,NC]

Rewrite request to a sub directory

I have a web application in a directory suppose xyz. I am trying to redirect all requests to a sub directory www. My current .htaccess code is:
RewriteEngine On
RewriteRule (.*) /www/$1 [L]
But, when I visit http://example.com/xyz/some_url the request is redirected to http://example.com/www/some_url rather http://example.com/xyz/www/some_url which I want.
Update: Sorry, I forgot to mention the directory xyz as in my case is likely to renamed. So, this directory doesn't need to be hard coded in the rewrite rule
Place this rule in /xyz/.htaccess:
RewriteEngine On
RewriteBase /xyz/
RewriteRule ^((?!www/).*)$ www/$1 [L,NC]
If your DocumentRoot does not contain the xyz path then you'll need to specify in the RewriteRule.
E.g.: RewriteRule (.*) /xyx/www/$1 [L]
This should work or be close I think:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/([^/]+)$ $1/www/$2 [L]
Use a relative path and two .htaccess files
You need only remove the leading / to rewrite to a url relative to the current folder:
# /xyz/.htaccess
RewriteEngine on
RewriteRule (.*) www/$1 [L]
This will rewrite /xyz/foo to /xyz/www/foo.
To prevent redirect loops add a .htaccess file to the www subfolder e.g.:
# /xyz/www/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Adjust as appropriate.

url rewriting index.php

i have urls like
http://mysite.com/index.php?p=resources
http://mysite.com/index.php?p=resources&s=view&id=938
but i want urls like
http://mysite.com/resources
http://mysite.com/resources/view/938
instead of making hundreds of rewrite rules i wonder if it would be possible to just have one? Ive head this is possible by "getting the uri and splitting it into parts" and then just add a rewrite rule for index.php
but how? could someone give an example or link a tutorial
I happen to use this in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
This essentially calls index.php no matter what is requested. Then inside your PHP code you can look at $_SERVER['REQUEST_URI'] to get the URL and parse it accordingly.
The RewriteCond lines are there to exclude direct calls to files. In my case I don't want stuff like requests for js/css/image files to go through index.php for performance reasons.
Create a .htaccess file. Not somefilename.htaccess, it is simply named .htaccess.
Note: Your index.php and .htaccess file should be in the same directory.
Now try this on your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9\-]+)$ index.php?p=resources
RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/([0-9]+)$ index.php?p=resources&s=view&id=938
</IfModule>
see more about url rewrite here

.htaccess file modification

I have a .htaccess file which arranges that all requests go through index.php.
Now i would like to make an exception for rss.php. to go straight throuh rss.php.
How do I do this?
This is how it looks like now:
RewriteEngine on
RewriteBase /
RewriteRule !\.(js|ico|txt|gif|jpg|png|css)$ index.php
Thanks.
Put this before the last line.
RewriteCond %{REQUEST_URI} !^/rss\.php$
You can exclude any existing file with an additional RewriteCond directive:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Or alternatively do a rewrite that matches the file then skips the rest of the rewrite tests.
By putting the following line before your existing RewriteRule will redirect without going through index.php.
RewriteRule ^/rss.php /rss.php [L]
I came across this page while hunting for a way to do this with my /robots.txt file.
mod_rewrite with apache1.3

Categories