Code Snippet
Subdirectories URL Internally Redirect to Query String
The URL in the browser would be:
http://css-tricks.com/index.php/teachers/a/
The actual page rendered by the server would be:
http://css-tricks.com/index.php?search=teachers&sort=a
RewriteEngine on
RewriteRule ^index/([^/]+)/([^/]+).php /page.php?search=$1&sort=$2 [NC]
I think you messed up a little, the URL in browser example wouldn’t match the Rewrite Rule. The URL in browser example would have to be “http://css-tricks.com/index/teachers/a/”
I tried playing with it myself, and this works:
http://css-tricks.com/examples/Rewrite/index.php/teachers/a/b/
And this doesn’t:
http://css-tricks.com/examples/Rewrite/index/teachers/a/b/
Oh, I think the only reason it works for me is because I I have rewrite rules that lets me take out the .php extension.
I like the idea of a clearer URL, but still I think keeping ‘index’ or ‘index.php’ as part of the URL has very little added value. I’d prefer something like http://css-tricks.com/teachers/a/ instead.
Its a clearer URL and improves SEO more importantly.
Slight problem. If you want to link to something deeper than the root of your website, it would be passed as an argument in index.php.
Instead, use a keyword like “search” to give yourself more latitude. The resulting http://css-tricks.com/search/teachers/a/ is SEO-friendly and you only limit yourself to avoiding a folder named “search”.
Nice one, thanks chris.
Its what I was finding for as it is very useful in php based website and seo friendly too.
The Reference URL is giving the full details. Thanks for providing it too.
Hey, Chris!
I’m struggelin and messing around these .htaccess, but I couldn’t write them as they work. I have mod_rewrite enabled, and I double checked that RewriteEngine is on at .htaccess file.
In example, I have one project, where, i have sections and categories, and link looks like this: http://localhost/eshop/index.php?section=fruits&category=apple
How I could rewrite this link to get it look like this: http://localhost/eshop/fruits/apple
And also I have similar situation inside WordPress.
I’ ve made theme, where I made a new engine for some kind of inside social network, but anyway.
For example, link looks like this: http://localhost/liepa6vsk/klases/?classroom_id=5&page=gallery
I would like to make it appear as http://localhost/liepa6vsk/klases/classroom_name/gallery
What I should write in .htaccess to do that, and still not crush previously written Rewrite rules for wordpress?
One last question: Does link will appear nice, when in link will be only http://localhost/liepa6vsk/klases/?classroom_id=5 (as http://localhost/liepa6vsk/klases/classroom_name/)
If you gonna solve my questions, you will be like God for me :)
@Robertr: the following code in your .htaccess file will fix the first problem:
RewriteEngine on #unless the rewrite engine is already declared
RewriteBase /eshop/ #if you dont have a rewrite base, it can cause issues with sub-directories.
RewriteRule ([^/]+)/([^/]+) index.php?section=$1&category=$2 [NC]
What if we want ?site={siteslug} to redirect to *.mysite.com
htaccess file works only on apache server, but i am currently using ASP that doesn’t run this file through IIS.. so any other ways to rewrite my URL??
You can also just use
$_SERVER["PATH_INFO"]to get everything after/index.php/.thanks everybody
can any one suggest, what should i do, if i want the usernames to be a part of url..
such as http://localhost/animeshkumar/
instead of http://localhost/profile.php?userID=1
where userID : 1 has the username : animeshkumar in the database..
You would have to have your php search for the username instead of the id.
http://localhost/animeshkumar/
How to remove the php extension on go daddy server?
OptionMultiviews On does not work for me..:(
“How to remove the php extension on go daddy server?”
Hi Amit Yadav,
It doesn’t matter who is the hosting provider.
It only matters whether your web host has installed Apache server or not.
Apache sever supports dynamic URL rewriting technique (ModURL Rewrite)
RewriteEngine on
RewriteRule ^index-([^/]+)-([^/]+).php /search.php?search=$teachers&sort=$ohio [NC]
Using this above code will rewrite the query string URL http://yoursite.com/search.php?search=$teachers&sort=$ohio into
http://yoursite.com/index-teachers-ohio.phpDigWP
A book and blog co-authored by Jeff Starr and myself about the World's most popular publishing platform.
Quotes on Design
Design, like Art, can be an elusive word to define and an awfully fun thing to have opinions about.
HTML-Ipsum
One-click copy to clipboard access to Lorem Ipsum text that comes wrapped in a variety of HTML.
Bookshelf
Hey Chris, what books do you recommend? These, young fertile mind, these.