Creating friendly URL's in Xampp - php

This is my non-friendly URL: localhost/website/users/?name=UserA
Now, i tried to create a friendly URL using .htaccess file.
This is everything in my .htaccess file:
ErrorDocument 404 /movies/error404.html #For Error 404
RewriteEngine On
RewriteRule /website/users/(.*)$ /website/users/name=$1
Now, When I open localhost/website/users/UserA it opens error 404 page.
Yes mod_rewrite is enabled

Try this in your .htaccess file
#Options +FollowSymlinks
RewriteEngine On
RewriteRule ^users/([0-9A-Za-z]+)/?$ users/index.php?name=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
ErrorDocument 404 http://localhost/movies/error404.html
In this cause the .htaccess file would be placed under the website directory root
Note: The requests are going to index.php file which is under the users directory if you have another filename then replace it with that one

Related

ErrorDocument 404 .htaccess not working with RewriteRule

.htaccess ErrorDocument 404 page not redirecting when I was using the rewrite rule. Getting 500 Internal Server Error.
Without RewriteRule the ErrorDocument is Working fine.
My Code
RewriteEngine On
<IfModule mod_rewrite.c>
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /stores/$1 [NC,L]
ErrorDocument 404 /404.html
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /stores/$1 [NC,L]
The "problem" with this rule is that if you make a request for a non-existent file and that file not exist in the /stores/ subdirectory either then it will result in an endless rewrite-loop (hence the 500 Internal Server Error you are seeing) since it will attempt to rewrite as follows:
/not-exists (initial request)
/stores/not-exists (1st rewrite)
/stores/stores/not-exists (2nd pass)
/stores/stores/stores/not-exists (3rd pass)
etc.
You've not actually stated what you are trying to achieve here, but I assume /stores is a "hidden" subdirectory and you are intending to rewrite to actual files within the /stores subdirectory. This is the only way a rule like this would work with an ErrorDocument 404 directive defined in Apache.
So, when rewriting to the /stores subdirectory, you need to first check that the request would map to an actual file before issuing the rewrite. That way, any request that does not exist and does not map to a file in the /stores subdirectory will drop through to the Apache defined 404 ErrorDocument.
For example, try the following instead:
ErrorDocument 404 /404.html
RewriteEngine On
# Rewrite request to "/stores" directory if file exists in that directory
RewriteCond %{DOCUMENT_ROOT}/stores/$1 -f
RewriteRule (.+) stores/$1 [L]
# (OPTIONAL) Rewrite requests for root to "/stores/"
RewriteRule ^$ stores/ [L]
The <IfModule> container is not required (but you had the RewriteEngine directive outside of this container - which defeats the point anyway).
The RewriteBase directive is not required here if the .htaccess file is located in the document root.
It is structurally better to define your ErrorDocuments first.

.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

CodeIgniter not working in sub-directory

I'm using CodeIgniter 3.1.1 .
The problem is when i browse sub-directory(http://www.wap.com/demosite) where CodeIgniter installed, it always redirect to main domain and i think the problem is in .htaccess.
For example :
Domain : http://www.wap.com
Main site directory public_html/
And .htaccess file in main-directory public_html/.htaccess
RewriteEngine on
RewriteOptions inherit
ErrorDocument 500 /errors/404
ErrorDocument 401 /errors/404
ErrorDocument 403 /errors/404
ErrorDocument 404 /errors/404
RewriteCond %{HTTP_REFERER} !^http://wap.com/.*$ [NC]
RewriteRule .*\.(jpg|jpeg|gif|png|bmp|js)$ - [F,NC]
CodeIgniter installation directory
public_html/demosite/
And .htaccess file in sub-directory public_html/demosite/.htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
I'm assuming the move is a new addition?
Need to make sure that in application/config/config.php that your base_url is set to http://www.wap.com/demosite not to http://www.wap.com
This is often missed and causes these kinds of redirects
Your codeigniter htaccess should also be set to RewriteBase /demosite/

No Framework >> .htaccess >> RewriteRule gives 404 Error

I am stuck at something that might seem nothing less but stupid - stuck with a .htaccess RewriteRule.
The Problem:
I am trying to redirect all traffic that comes to the "project" webistes' "non-existant" pages to the index.php as a parameter "q". For example, lets say a user has come to the following URL:
http://localhost/project/products/
Now obviously there is no directory or file called products, hence,
For the above HTTP request, I want to translate this as:
http://localhost/project/public/index.php?q=products
The Setup:
Server - Apache 5.3.x with XAMPP
Directory Structure : htdocs/project/public/index.php
What I have already done in an attempt to solve the problem:
Placed a .htaccess file in project directory >> http://localhost/project/ >> in an attempt to transfer all the traffic to the public directory. The rule for that looks like this...
<IfModule mod_rewrite.c>
RewriteRule ^$ public/ [L]
</IfModule>
Above thing is working and all users getting redirected to the index.php page.
Placed another .htaccess file in the public directory with the follwing code:
<IfModule mode_rewrite.c>
RewriteCond....
RewriteCond....
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
Now if a user types the URL at the bar: http://localhost/project/hi
I want it to be translated as http://localhost/project/public/index.php?q=hi
Instead of that it is giving a 404 Error. Also please consider that the actual request being translated to the Apache should not be reflected in the URL bar as a permanent redirect [301].
Does anybody have a solution ?
Your htaccess isn't on the server root.
Then you must define the base url, and use a full path for your rewrite rule :
RewriteEngine on
RewriteBase /project/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /project/public/index.php?q=$1 [L,QSA]
If that file index.php is located at /public/ than you should do
<IfModule mode_rewrite.c>
RewriteCond....
RewriteCond....
RewriteRule ^(.*)$ /project/public/index.php?q=$1 [L,QSA]
</IfModule>

how can I set .htaccess for existing files and folders

Can any one help me in .htaccess.
My use case is, if there is index.php in the directory http://domain_name/user/ then load index.php else set rewriteRule to call forbidden.php.
I did a bit but not succeed yet. .htaccess code on my server is-
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond ^(.*)$/index.php !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.*)$ forbidden.php?handle=$1
</IfModule>
I think there is an easier way to do what you want. There is no need for rewriting, simply define which page acts as the entry page. In the .htaccess file you can define:
DirectoryIndex index.php
ErrorDocument 404 /forbidden.html
Now, if anybody calls your directory http://www.example.com/user/, the page http://www.example.com/user/index.php will be shown.
If the file index.html does not exist, the server will return an error, so you can define an appropriate error page. With a leading /, you can define a single error page in the root directory, without the / it will look in the relative directory http://www.example.com/user/forbidden.php.

Categories