URL Re-Writing with .htaccess in custom php (xampp) on windows 10 - php

I want to re-write the following url from:
www.phcc.com/reports/caseFullDtlByCaseSno.php?case_sno=1
To
http://www.phcc.com/reports/1/caseFullDtlByCaseSno.html
For this i have written the following .htaccess file inside the reports directory which is the sub-directory of the website root:
RewriteEngine on
RewriteBase /reports/
RewriteRule ^(.*)/(\d+)\.php $1=case_sno&$2.html [NL,L]
RewriteRule ^(.*)/(\d+)\.html $1=case_sno&$2.php [L]
but it does not complete the request as i need it.
What should i do?

Try with below, I am assuming the html files are not existent.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\d]+)/([\w-]+)\.html$ $2.php?case_sno=$1 [L]

Related

.htaccess error : The requested URL was not found on this server

I have a php file which is home.php
my url on localhost is
http://localhost:8888/photo/home.php
I want to remove .php from the url
so i create .htaccess file
RewriteEngine On
RewriteRule ^home?$ home.php
but i keep getting this error
The requested URL /photo/home was not found on this server.
First of all, ensure your .htaccess file is in the base directory of your project. Next, follow these htaccess rules to set up your redirect.
The .htaccess solution you might be looking for is:
RewriteEngine On
#Neither a file nor a directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^photo/home$ photo/home.php [QSA,L]
Try it like this, in your photo directory.
RewriteEngine On
# neither a file nor a directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^home$ home.php [QSA,L]
I got this problem when I put the .htaccess code in the root .htaccess. It works when I create a new .htaccess in the subdomain folder on the same level with public_html folder and fill it with the RewriteRule needed

How to replace ?query with /query

I'd like to make it so when someone does example.com/page.php/query it works as a $_GET (example.com/page.php?q=query) for the query
How would I do this?
Would this be done with the page.php's code or would I do it in .htaccess?
In order to route a request like /test/something to internally rewrite so that the content at /test.php?whatever=something gets served, you would use these rules in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?test/(.*?)/?$ /test.php?whatever=$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

shift folder in url via htaccess

imagine the following situation:
i've got a folder on the webserver:
www.domain.com/test/project
in "project", i've got my whole php application. in "project" i've also got the following .htaccess file:
RewriteEngine On
rewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(util/(css|js))($|/) - [L]
rewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(UI/designs)($|/) - [L]
RewriteRule ^(.+)$ index.php?uri=$1 [QSA,L]
which allows me basically to have url like this for example:
www.domain.com/test/project/admin/user
i've allowed access to "util/(css|js)" for loading the css and js files from ther
but eventually i want to have this url for example:
www.domain.com/test/admin/user
so basically, to just shift the "project" folder....
is this possible with an .htaccess file?
is this also possible with the following example:
www.domain.com/project/admin/user
www.domain.com/admin/user
please help me, i often tap into 500 internal server error :(
i tried something like this:
RewriteRule ^test/(.+)$ http://www.happydomain.org/folder/$1 #[R=301,L]
i've got the solution! =)
this .htaccess must be adden in the "test" folder - concerning my example mentioned in the question
RewriteEngine On
RewriteCond %{REQUEST_URI} !^project/
RewriteRule ^(.*)$ project/$1 [L]

How to rewrite URL to include variables and remove php while allowing directories

I have the following htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/?user=$1
which works as expected for example
http://example.com/index.php?user=USERNAME -> http://example.com/USERNAME
However I have created a form on the page index.php which posts to /directory/save.php
How do I remove .php while allowing for the directory so that I can post to /directory/save/ instead?
if it is going to one and only such file in /directory then probably hard code it by adding following before the above rules?
RewriteRule ^directory/save$ /directory/save.php [L]
directory/ might have it's own rewrite rules and not have a physical save.php, that's why !-f might not work. Try adding a new rewrite condition to stop rewriting for directory/
RewriteCond %{REQUEST_URI} !^directory
Try to check this one if it's rewriting /directory/save.php file to directory /directory/save/
RewriteCond %{REQUEST_URI} ^/directory/save\.php$
RewriteRule ^(.+) /directory/save/ [NC]

Categories