clean url with mod_rewrite - .htaccess

I found several topics on mod_rewrite on stackoverflow but none them solved my problem. I also tried tutorial on this website http://edrackham.com/apache/beginners-mod_rewrite-tutorial/
I have been trying to use mod_rewrite to get clean url. For example:
www.myweb.com/itemdetail.php?itemid=111234
to
www.myweb.com/itemdetail/111234 or
www.myweb.com/itemdetail/iphone
This is my .htaccess file content.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %(REQUEST_FILENAME) !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
As suggested in stackoverflow, I added following rewriterule to above file
RewriteRule ^itemdetail/([0-9]+)/$ itemdetail.php?itemid=$1 [NC,L]
But this did not solve my problem. Any suggestion is appreciated.

You should make the last slash optional :
RewriteRule ^itemdetail/([0-9]+)/?$ itemdetail.php?itemid=$1 [NC,L]

On some servers you can't use a slash in a RewriteRule (I have the same problem). To check this try the following as your htaccess file:
RewriteEngine on
RewriteRule ^itemdetail-([0-9]+)/?$ itemdetail.php?itemid=$1
EDIT:
Of course your permalink would be www.myweb.com/itemdetail-111234

Related

Rewrite url for seo friendly htaccess

I want to rewrite this in SEO friendly url.
From - www.example.com/section.php?name=website
To - www.example.com/section/website
I tried this:
RewriteEngine On
RewriteRule ([^/\.]+)/([^/\.]+)?$ section.php?type=$1&service=$2 [NC,L]
I am fetching data using GET method in section.php and pass to the another page but after opening sub folder like www.example.com/{foldername} it returns to the section.php
Thanks. I will appreciate your help.
With your shown samples, please try following htaccess Rules file. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules for rewrite to section.php as per OP's request.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ section.php?name=$1 [L]
This helps me
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ([^/\.]+)/([^/\.]+)/?$ search.php?type=$1&service=$2

How to rewrite two GET values on htaccess and mod-rewrite

I just want to make the GET information that I sent:
http://mywebsite.com/folder/index.php?category=category-alias-here
http://mywebsite.com/folder/index.php?category=category-alias-here&page=5
To be rewrited on the URL bar as:
http://mywebsite.com/folder/category-alias-here/
http://mywebsite.com/folder/category-alias-here/5/
And I cant. Its very simple, I saw several similar questions. Tested several examples on stackoverflow and they all give me 404 for both category and both pages.
Here is a code that I tested:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?category=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?category=$1&page=$2 [L]
I understand that this code be wrong for my structure specifically, I tried several changes placing and removing ([^/]+) or changed the fist [L] to [N]. And several other things. But no luck with that.
Please notice, the whole website is inside a real directory (/folder/) and the .htaccess is also there (its not on root), once I launch the website everything will be on public_html, but I do not think this is the reason of this issue.
And yes, mod_rewrite is on. There is actually a .htaccess that I use, it works anywhere I place and it gets the category correctly but it is a bit messy.
Found a solution:
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-z-]+)\/([0-9-]+)\/$ index.php?category=$1&page=$2 [NC,L]
RewriteRule ^([a-z0-9-]+)\/$ index.php?category=$1&page=$2 [NC,L]

Remove index.php from the url when using the CakePHP pretty URLs

I want to remove the index.php from the url: http://mywebsite/index.php/.
I m using CakePHP pretty URLs, I removed all the .htaccess files:
/.htaccess
/app/.htaccess
/app/webroot/.htaccess
and uncommented the App.baseUrl on the core.php.
Thank you for your help!
you can try this. i have been using this for my codeigniter and i think this will work for you too.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L,NC]
oh you dont want to use htaccess? . i misundestood your question.

Clean and SEO Friendly URL with HTACCESS file

Now i use this code for my website.
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This code convert all url of my website:
www.example.com/category/work.php
in
www.example.com/category/work
It's great, but my goal is:
www.example.com/category/work.php
in
www.example.com/category/work/
I need "/" at the end.
Is possibile? This is a typical feature of CMS.
I want it in my site built by me.
You can force trailing slash
RewriteRule ^((.*)[^/])$ $1/ [L,R=301]
Also check this Stackover answer

.htaccess exclude specific file in webroot from URL rewriting

i am running a site with joomla. It has a bunch of mod rewrite rules i don't totally understand.. as well as some kind of SEF link thing running.
i have a specific page named sched.html that i need to serve up and be completely ignored from the .htaccess.
I have tried everything i can possibly think of. including a number of recommendation from people in the Joomla forums and for the life of me i cannot get this working.
ANY help is appreciated!
You should have that line already in your .htaccess:
RewriteRule (.*) index.php
If not, post your complete .htaccess here.
Prepend this line with the following:
RewriteCond %{REQUEST_URI} !/sched\.html
The final result should look similar to this code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteCond %{REQUEST_URI} !^/sched\.html
RewriteRule (.*) index.php

Resources