How to resolve following htaccess code - php

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
can any body explain me how above htaccess rule works
e.g. if I have URL http://mydomain/edituser
so what php script will match with given URL
earlier I write different rules for each given URL
but in above case how I know that witch php script get run
please help me

That rewrite rule matches any request that does not match an existing file, and routes the request to to index.php using PATH_INFO

Translation of the above code is like that:
RewriteEngine On: Activate the RewriteEngine if not already activated
RewriteCond %{REQUEST_FILENAME} !-f: If the requested file name is not a regular file
RewriteCond %{REQUEST_FILENAME} !-d: If the requested file name is not a regular directory
RewriteCond $1 !^(index\.php|images|robots\.txt): If the request is not the index.php, the images or robots.txt file
RewriteRule ^(.*)$ /index.php/$1 [L]: Send the request to index.php and stop ([L])

This looks as a part from WordPress. If the file doesn't exists (-f) and a directory also not exists (-d) and the request is not for index.php or images or robots.txt when call index.php with the path as a parameter.

Related

How to write url rewrite with OR operator?

This is file structure of the site :
site_one_folder
index.php
In site_one_folder there is a file:
controller.php
I want to write rewrite rule to
If people search www.mysiteurl/index.php or www.mysiteurl or www.mysiteurl/, it will run index.php
If people search instead of index.php :
example: I will search
www.mysiteurl/something.php
Then Browser will show the above url but content will have to be
`www.mysiteurl/site_one_folder/controller.php?para=something`
NB: para = searched file name excluding (.php)
Try adding this to the htaccess file in our root folder (where index.php is):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)\.php$ /site_one_folder/controller.php?para=$1 [L,QSA]
Make sure mod_rewrite is enabled.
1st condition for direct access to index.php. 2nd to avoid infinite redirection
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^/site_one_folder/controller.php$
RewriteRule ^(.+)\.php$ site_one_folder/controller.php?para=$1 [L]

Creating SEF urls using .htaccess

How can I change a url of the following format
example.com/page/1
To
example.com/index.php?page=1
?
When I enter
example.com/page/1
it should redirect to
example.com/index.php?page=1
What changes do I need to do in my .htaccess file?
folder structure is as follows
-Public_html
.htaccess
index.php
Thanks.
Use this in your public_html/.htaccess file
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([0-9])/?$ /index.php?page=$1 [QSA,NC,L]
RewriteCond checks if the requested filename or directory already exist, RewriteRule will be skipped.
You can put this code in your htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([0-9]+)$ index.php?page=$1 [NC,L]
With only this code, you can now access http://example.com/page/55 and see the content of /index.php?page=55 for instance.
But... the thing is, you're still able to access http://example.com/index.php?page=55 and it creates a duplicate content: very bad for referencing (Google, and others).
More info about duplicate content: here.
Solution: you can add another rule to redirect http://example.com/index.php?page=55 to http://example.com/page/55 (without any infinite loop)
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/index\.php\?page=([0-9]+)\s [NC]
RewriteRule ^ page/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([0-9]+)$ index.php?page=$1 [NC,L]
Note 1: make sure (in Apache configuration file) that you've enabled mod_rewrite and allowed htaccess files.
Note 2: since your rule creates a virtual directory (/page/) you'll have some problem if you're using relative paths for your html resources. Make sure all your links (js, css, images, href, etc) begins with a leading slash (/) or instead, add a base right after <head> html tag: <base href="/">
in my answer I assume you are using linux,
I also assume you will have more complicated cases such as more than
one parameters you will want to catch
example.com/page/1/3
in this case I think you will have to use parsing the url in your index.php
first you will have to setup the .htaccess file in your site root, also you will have to make sure mod_rewrite is enabled in your apache configuration
in case you are running debian you can run this command in terminal
to make sure this mod is enabled:
sudo a2enmod rewrite
add htaccess file to the root of where your index php file is located example:
/var/www/html/.htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
</IfModule>
according to my solution in your index.php you must have function that can parse the url request
/*
$base path is used only if you running your site under folder
example.com/mysitefolde/index.php
*/
function getUrlParams($basePath = ''){
$request = str_replace($basePath, "", $_SERVER['REQUEST_URI']);
return explode('/', $request);
}
index.php request to example.com/page/1/2
$request = getUrlParams($rootPath);
$module = $request[0]; // page
$moduleValue = $request[1]; // 1
$moduleValue2 = $request[2]; // 2

.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]

htaccess add .html extension for urls with or without trailing slash

So to begin with I have a custom url rewrite that sends a request variable to a php script
Rewrite rule is below:
RewriteRule ^([\w\/-]+)(\?.*)?$ test/index.php?slug=$1 [L,T=application/x-httpd-php]
So if you access something like domain.com/slug-text it sends slug-text to index.php located in folder named test.
What I want is all my urls to look like domain.com/slug-text.html, but slug-test variable should still be sent to index.php file.
And
What I can't figure out is the redirect. I want all the old urls to be redirected from domain.com/slug-text or domain.com/slug-text/ to domain.com/slug-text.html and slug-text sent to index.php file located in test folder.
Searched a lot but could not find the answer for this question anywhere on the Internet.
Thank you all for the help.
UPDATE:
my new code is:
RewriteEngine On
Options +FollowSymlinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(([\w/\-]+)?[\w-])(?!:\.html)$ http://domain.com/$1\.html [L,R=301]
RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]
domain.com/slug-text/ does not get redirected to domain.com/slug-text.html
domain.com/slug-text works as intended redirecting to domain.com/slug-text.html
What do i need to change?
This rule:
RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]
Will trap domain.com/slug-text, domain.com/slug-text/ and domain.com/slug-text.html and send slug-text to /test/index.php inside slug param.
If you really want to redirect using [R=301] from old urls to new then use this:
RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]
Also note that as using explicit redirect bottom rule is modified to trap url's ending with .html
It is also advisable (if your .htaccess does not already contain this) to filter conditions for existing files and folders not to be trapped by your redirect rules. Simply add these lines before RewriteRule lines:
# existing file
RewriteCond %{SCRIPT_FILENAME} !-f
# existing folder
RewriteCond %{SCRIPT_FILENAME} !-d
And if using symlinks:
# enable symlinks
Options +FollowSymLinks
# existing symlink
RewriteCond %{SCRIPT_FILENAME} !-l
// addition
Your .htaccess file should look like this:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]
This is supposed to redirect /slug-text to /slug-text.html
RedirectMatch ^/([\w-]+)/?$ http://domein.com/$1.html
This is in the case when slug-text is only letters, digits, – and _.
Rewrite slug-text.html to a php file and pass the slug as a param:
RewriteRule ^([\w-]+)\.html$ test/index.php?slug=$1 [R,L]
If you have both line in your .htaccess the first one will do the redirects from the legacy URLs to the new ones and the second one will process the request.

Wordpress URL Parameters on GoDaddy

I am currently making a website (http://tannernelson.me/ehs)
It's hosted on Godaddy, and I'm using wordpress as a CMS.
I want to be able to make:
http://tannernelson.me/ehs/school/academics/teachers/jsmith
turn into
http://tannernelson.me/ehs/index.php?pagename=teachers&detail=jsmith
So, basically, if there are 4 segments to the url (school/academics/teachers/jsmith) I want the last one to be a variable. So the fourth segment of any url will be the variable "detail"
My current URL rewrite is currently
# Mod Rewrite
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /ehs/index.php [L]
Options -Multiviews
It won't work any other way, even with the default WordPress .htaccess file. And I have no idea what that means, or what kind of request URI is made out of it. It's really confusing.
Any ideas?
The code you have right now means:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
If the requested filename is not (!) a regular file -f and if the requested filename is not (!) a directory -d then:
RewriteRule . /ehs/index.php [L]
Match any single character (.) and if a match is found rewrite the URL to /ehs/index.php and then make this the last rule ([L]) so don't process any further rules.
This doesn't look like what you want, but seems to be working. http://tannernelson.me/ehs/school/academics/teachers/jsmith serves up (I think) http://tannernelson.me/ehs/index.php because I get a custom 404 not found page.
Try the following .htaccess code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Redirect the ehs/school/academics/$1/$2 URIs to /ehs/index.php?pagename=$1&detail=$2
RewriteRule ^ehs/school/academics/([^/]+)/([^/]+)$ /ehs/index.php?pagename=$1&detail=$2 [L]
# Otherwise if the requested URI is not found (not a file nor a directory)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#Redirect everything else to index.php
RewriteRule .* /ehs/index.php [L]
Options -Multiviews
I just tested this on my Apache server and it works.

Categories