How to change Our website URL using htaccess - php

I'm new in PHP so I faced some problem using URL redirect using .htacess.
My URL page look like this: http://domain.com/blog_detail.php?id=blog_title
But I want change URL using .htaceess like this: http://domain.com/blog/blog_title
I have tried this, but it's not working:
<IfModule mod_rewrite.c>
RewriteEngine on # Turn on the rewriting engine
RewriteRule ^blog/([a-zA-Z0-9_-]+)$ blog_detail.php?id=$1
</IfModule>

I would use:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f ## respect real files
RewriteCond %{REQUEST_FILENAME} !-d ## respect real directories
RewriteBase /
RewriteRule ^blog/(.*)$ blog_detail.php?id=$1&%{QUERY_STRING} [L]
&%{QUERY_STRING} only if you want to pass other variables here and there like:
http://domain.com/blog/blog_title?lang=fr
for instance

Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^(.*)$ blog_detail.php?id=$1 [QSA,L]
</IfModule>

Related

How do i shorten my url like facebook do to users profile page

I am trying to shorten my website url of a newly added page event like
mysite.com/asoebi/myevent instead of mysite.com/asoebi/index.php?event=myevent
but every time i try to change my .htaccess file it give an error
here is .htaccess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^index/(\w+)$ ./index.php?event=$1
</IfModule>
You have to enable mod_rewrite in apache for this to work.
Put this one in the .htaccess file in the /public_html/asoebi/ folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /asoebi/
RewriteRule ^(.*)$ index.php?event=$1 [L]
</IfModule>
Also remove the last rewriterule from the main htaccess file you posted above

PHP (htaccess) rewrite with $_GET request

I made an simple framework for my website development.
I am currently using the this URL pattern:
www.example.com?r=account/login
But I would like to change it to:
www.example.com/account/login
remove r= from the URL.
I have seen YII framework does this. But I couldn't find a way to do this.
Solution:
Add the code below to your .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ index.php?r=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ index.php?r=$1
</IfModule>`
What you are looking for are SEO friendly URLs which are quite common to use & implement. This is the .htaccess from a basic WordPress install which achieves a similar goal:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Then in index.php you can test this by doing a variable dump of $_GET like so:
echo '<pre>';
print_r($_GET);
echo '</pre>';
I see you found a solution however you don't/shouldn't need two rewrite rules for the same thing just to add the /. You can use a ? to make it optional. I also recommend using [L] on the rewrites so that it stops when the rule is met.
This is more of what you were looking for.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?r=$1 [L]
</IfModule>

beginners guide for php htaccess

I have just started learning htaccess.
And i want to test one page :
I have one folder called "favs" and inside that i have a php file index and from that i am passing to the page gallery.php with querystring ID
so the url will be :
mainsite.com/favs/gallery.php?id=7
Now i want the url to be like :
mainsite.com/favs/7
and this is the code i write in .htaccess file :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ gallery.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ gallery.php?id=$1
</IfModule>
Your rules are wrong. You have this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ gallery.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ gallery.php?id=$1
</IfModule>
^([a-zA-Z0-9_-]+)$ will match everything in the URL after the first slash, as long as there is no slash. There's a slash in mainsite.com/favs/7, so the rule doesn't match, and it looks for an actual directory named /favs/7. What you want is to match only the piece after favs, like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^favs/([0-9]+)/?$ favs/gallery.php?id=$1
</IfModule>
The code above is what you should put in .htaccess if it is in your root directory. If it is in your /favs directory, then you need to remove the /favs from RewriteRule and add a RewriteBase like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /favs
RewriteRule ^([0-9]+)/?$ gallery.php?id=$1
</IfModule>
Note: based on your question, I have eliminated letters, _, and - from the regular expression. If you need to match them, you can tweak the rule above accordingly.
the is my full code :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteEngine On
RewriteRule ^favs/([0-9]+)/?$ favs/gallery.php?id=$1
</IfModule>
and this is the url :
http://www.fullridescholarships.net/favs/

Mod_Rewrite for clean URL

Really simple request. I want my url to look like:
http://localhost/movie/72105
instead of this
http://localhost/movie?id=72105
I currently have this setup going on in my .htaccess file, but it does not seem to be working:
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([a-z\-]+)$ index.php?id=$1 [L]
</IfModule>
Just to clarify aswell, when the mod_rewrite is working successfully, I can still use to retrieve the ID?:
$_GET['id']
Much appreciated for any help.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^lab/movie/(\d+)$ index.php?id=$1 [L]
</IfModule>
Now, you can browse http://domain.com/movie/12 and $_GET['id'] will be 12.
I am using this one
Options +FollowSymLinks
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
RewriteBase /
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
you can retrive the id and other parameters (mobvie) by parsing
$_SERVER['REQUEST_URI']
in your index.php than.
also see: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

HTAccess URL Rewrite affect GET variables

We’re using wordpress, and we have the following in the .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
We have a URL like http://subdomain.example.com/select-a-card/?q=fuel, and we want to rewrite it so that it becomes something like http://subdomain.example.com/fuel-card/
What do I need to add to the htaccess to do this? Also, will this affect the queries from running that uses GET variables?
nothing, just read the wordpress manual and edit your settings. checkout http://codex.wordpress.org/Using_Permalinks
--- EDIT
This can be done, but only if you have some sort of a standard markup of the url's you want to have rewritten. Ie. if you only want to rewrite the select-a-card url's, or only the url's with a syntax like http://site.com/uri/?q=something. But you if the syntax of the url's differ too much you will have to add a lot of rewrites.
In this specific case something like this should work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# add your specifics below:
RewriteRule ^/select-a-card/\?q=(.*) $1-card/
RewriteRule . /index.php [L]
</IfModule>

Categories