Using .htaccess to rewrite a simple url - php

I have a url like this - www.example.com/blog.php/username, it will take you to the username's blog page, but I want to use a url like this instead - www.example.com/blog/username to get to the same file (blog.php). Please what are the steps I need to take to achieve this? Thanks for any help.

Create a file in your root direstory named .htaccess and past this in :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^blog/(.*)$ yourpage.php?username=$1 [L]
</IfModule>
(.*) will take any caracters
If you want to use only letters and numbers change (.*) by ([A-Za-z0-9])

Enable mod_rewrite and .htacess. Then add this code in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
# finally this is your rewrite rule
RewriteRule ^blog/(.+)$ blog.php/$1 [L,NC]

Removing file extension:
There are two ways to do this. The easy way which just looks at a
request, if the requested filename doesn't exist, then it looks for
the filename with a .php (or .asp or whatever) extension. In an
.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
Now if you go to: http://domain/about the server will interpret it as
if you went to http://domain/about.php.
Makes sense, but if we're already breaking the relation between URL
and filename, we may as well break it intelligently. Change that
.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Source: Dave Dash -> http://davedash.com/2006/07/26/how-to-remove-file-extensions-from-urls/

1 You should install mod-rewrite
2 You should make sure that for your doc root directory is "AllowOverride All". Ordinary after virtualhost configuration you should find something like:
<Directory "/your/doc/root/path/">
AllowOverride All
</Directory>
3 After that you should start rewrite engine and create rewrite rule in your .htaccess as David BĂ©langer advises:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^blog/(.*)$ blog.php/$1 [L]
</IfModule>

Related

htaccess file has no affect

I'm trying to rewrite an url like this:
www.mywebsite.com/product/a-product-name-here/92341
to
www.mywebsite.com/our-products/a-product-name-here/92341
I'm using following rewriterule:
RewriteRule ^product/(.*)$ our-products/$ [R=301,L]
And according to this online tool it should do the trick. I've put this online, but it didn't affect anything.
AllowOverride is active on my host.
My folder structure is like this (tried the rewrite rule in both of the 2 .htaccess's):
UPDATE
This is the .htaccess content in the folder Corporate
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /corporate/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /corporate/index.php [L]
</IfModule>
# END WordPress
And this is the content of the .htaccess file in the product folder:
AddDefaultCharset utf-8
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.+) index.php?a=$1 [nc]
Alright, as far as I understand, your problem can be divided into two parts.
Redirect public URL
You want the public URL example.com/product/foo-bar/1234 to be redirected to example.com/our-products/foo-bar/1234. This can be achieved with the already posted rule by anubhava, slightly modified to only match numbers:
Put it in your root .htaccess, besides index.php.
RewriteRule ^product/((.+)/(\d+))$ /our-products/$1 [R=301,L]
Map external URL to script, internally
Then, the external URL is example.com/our-products/foo-bar/1234, which does not physically exist on the server machine and therefore fails with a 404.
So you'll have to map it to the script containing the actual logic.
RewriteRule ^our-products/(.+)$ /product/index.php?a=$1 [L]
This is assuming that your script will accept the a parameter.
Your .htaccess files, after the changes
This is the .htaccess content in the folder Corporate
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^product/((.+)/(\d+))$ /our-products/$1 [R=301,L]
RewriteRule ^our-products/(.+)$ /product/index.php?a=$1 [L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /corporate/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /corporate/index.php [L]
</IfModule>
# END WordPress
And this is the content of the .htaccess file in the product folder:
AddDefaultCharset utf-8
Options +FollowSymlinks

Removing index.php in CodeIgniter using .htaccess file when using your own local domain

I have seen other articles on the same issue. But none of the answers have solved my problem.
I am not using xampp or wamp but have setup php mysql apache seperately. Also I am using my own local domain for instance mytestsite.com.
My .htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /TestCI/
# Canonicalize Codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(HomeController(/index)?|index(\.php|html?)?)/?$ / [R=301,L]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R=301,L]
# Enforce www
# If you have subdomains, you can add them to
# the list using the "|" (OR) regex operator
RewriteCond %{HTTP_HOST} !^(www|TestCI) [NC]
RewriteRule ^(.*)$ http://www.mytestsite.com/$1 [R=301,L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
I have also made following changes in config.php
$config['base_url'] = 'http://mytestsite.com/TestCI/';
$config['index_page'] = '';
$config['uri_protocol'] = 'PATH_INFO'; //AUTO
I have also made following changes in apache2.conf
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
But when i try to access
http://mytestsite.com/TestCI/HomeController/
its showing 404 error but works correctly when using
http://mytestsite.com/TestCI/index.php/HomeController/
I have looked into several similar questions that have been reported solved, but none of the answers have worked for me. Is there anything else that should be done to remove index.php from the url. Kindly respond back. Help highly appreciated.
It may be the htaccess isn't being used.
Put a sytax error in the file. (Append something like hdtbf to the file)
If that doesn't give a 500 error you need to turn allow override all
In your main Apache config
http://httpd.apache.org/docs/2.2/mod/core.html#allowoverride
Replace the following line of code in your .htaccess file
RewriteEngine On
RewriteBase /project/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Just put this code in your .htaccess :
RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Thankyou for helping me... Problem is solved.
I made the following changes in my apache2.conf file
<Directory /Absolute path to .htaccess folder
AllowOverride All
Require all granted
</Directory>
Also I removed the RewriteBase parameter in .htaccess file which is where the actual pbm was.
My current .htaccess file looks like
<IfModule mod_rewrite.c>
RewriteEngine On
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
I have also made changes in config.php where i changed $config['base_url'] to codeigniter root path

.htacess for removing public/index.php works good in root and not in sub directories

Here is my .htaccess which i have default in laravel it helps me to prevent typing public/index.php
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
So, if i need to access
localhost/public/index.php/home
localhost/public/index.php/login
It is enough to me to type
localhost/home
localhost/login
I want to make exception to for few directories such as 'testproject' and 'betaproject' i.e., where i will have a regular php or html directory or i will have another fresh project and it will have same .htaccess also.
How can i modify my .htaccess file to make it possible.
So that if i want to access
localhost/testproject/public/index.php/home should be accessible by localhost/testproject/home
In your root .htaccess have this rule to skip 2 directories:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(testproject|betaproject)/(\w+)/?$ $1/public/index.php/$2 [L,NC]
Make sure this rule is very first just below RewriteEngine On line.

how to fix url with htacces

I have a url which initially http://web.com/index.php/home/funct/ and I want to change this by removing the index.php so that it becomes http://web.com/home/funct/
If you are using Apache server, create a file called .htaccess and put this in it:
<IfModule mod_rewrite.c>
RewriteEngine on
# If the file/dir is NOT real go to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
</IfModule>
Then just remove the reference to "/index.php/" from your links and when the server doesn't find "home/funct" it will go looking for "/index.php/home/funct".

htaccess rewrite rule to redirect to file or not

I have been trying to write a set of htaccess rules that will do the following:
With the url:
mydomain.com/blog/something
If a file called blog.php exists in
the web directory(the directory with
index.php in) then redirect to
blog.php?url=something
If blog.php does not exist, redirect
to index.php?url=blog/something
EDIT: mydomain.com/blog/something is an example, it could be any url string and the htaccess should search the web directory for a file corresponding to the first part of the url string, which in this case is "blog"
In both cases I can then use $_GET['url'] to get parameters from the url to intiate whatever actions in my php script.
I currently have:
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)$ $1.php?url=$2 [L]
RewriteRule ^(.*)$ index.php?url=$1 [L]
</IfModule>
But unfortunately this is not working, I'm not sure why, I'm proficient enough with php but my htaccess skills are pretty limited. I don't know if the [L] option is the one I should be using or if I should be using more options. Currently whatever the url string is, $_GET['url'] returns "index.php".
If any more information is required just ask.
I hope someone can help
Regards
Luke
I hope that the following directives will work for you.
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?.*$ - [E=FILE:%{DOCUMENT_ROOT}/$1.php]
RewriteCond %{ENV:FILE} !^$
RewriteCond %{ENV:FILE} -f
RewriteRule ^([^/]*)/?(.*)$ $1.php?url=$2 [QSA,L]
RewriteCond %{ENV:FILE} !^$
RewriteCond %{ENV:FILE} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Apache mod_rewrite Reference: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

Categories