RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ product.php?ref=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /category.php?sub=$1 [L]
The values sent with variable sub conflicts with values from ref
product.php?ref=$1 [L]
category.php?sub=$1 [L]
My advice for any kind of rewrite rules that intend some form of semantic URLs, that is:
/home
instead of
index.php?page=home
Make one rule that captures all URLs, like:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ handler.php?__url=$1 [QSA]
Then have handler.php figure out what you want to do.
You know your categories and products, so if the URL is /[product|category] then handle it.
With little bit of change following code should work:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# don't do anything for a valid file or directory
RewriteCond %{ENV:REDIRECT_STATUS} !^$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Product handler /i/test
RewriteRule ^i/([^/]+)/?$ /product.php?ref=$1 [L,NC,QSA]
# PHP handler
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ /$1.php [L]
# category handler
RewriteRule ^([^/]+)/?$ /category.php?sub=$1 [L,QSA]
Related
I have an .HTACCESS file which allows a user to write the username of a user, on my social networking site in the URL.
I first implemented these rules to easily access specific users profile_page's by just modifying the URL, i.e. profile_page/freddy - Will take me to Freddy's profile page etc. And the rules were working.
I also have bio.php which is unique for each user. Therefore, I have written the exact same rules as I did for profile_page but just changed the file name. However, this does not work, I get an 404 error.
I have several pages similar to this, but the rules for profile_page are the only ones that work.
.HTACCESS in full:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^(me)?/?$ profile_page.php [L,NC]
RewriteRule ^profile(?:_page)?/([\w-]+)/?$ profile_page.php?u=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ profile_page.php?u=$1 [L,QSA]
RewriteRule ^(me)?/?$ photos.php [L,NC]
RewriteRule ^photos?/([\w-]+)/?$ photos.php?u=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ photos.php?u=$1 [L,QSA]
RewriteRule ^(me)?/?$ hearted_thoughts.php [L,NC]
RewriteRule hearted_thoughts([\w-]+)/?$ hearted_thoughts?u=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ hearted_thoughts?u=$1 [L,QSA]
RewriteRule ^(me)?/?$ favourited.php [L,NC]
RewriteRule favourited([\w-]+)/?$ favourited?u=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ favourited?u=$1 [L,QSA]
RewriteRule ^(me)?/?$ bio.php [L,NC]
RewriteRule bio([\w-]+)/?$ bio?u=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ bio?u=$1 [L,QSA]
Your rewriteRules are conflicting with one another because of the indentical rewrite patterns (
^(me)?/?$
and
^([w-]+)$
) in all rules.
edit your rules like this :
Options -MultiViews
RewriteEngine On
RewriteBase /
#--let existent files and dir pass untouched--#
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
################
#--rewrite /me to /profile_page.php--
RewriteRule ^(me)?/?$ profile_page.php [L,NC]
###############
#--Rewrite /profile/foo to /profile_page.php?u=foo--#
RewriteRule ^profile(?:_page)?/([\w-]+)/?$ profile_page.php?u=$1 [L,QSA,NC]
###############
#--rewrite /bio/foo to /bio.php?u=foo--#
RewriteRule ^bio/(.+)$ /bio.php?u=$1 [NC,L]
###############
#--other rules--#
So I had a rule the removed the necessity of having .php after my admin page, 'admin.php':
# Don't require .php extension to load php page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L]
The rule worked fine untill I needed to add the below rules;
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /?action=viewCategoryName&categoryName=$1&page_identifier=$2 [L,QSA,B]
and, the one effecting the first rule;
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ /?action=viewHomepageName&page_identifier=$1 [L,QSA,B]
Im wondering if how I've made the CMS and its query strings is lending to this being impossible to rectify. Could do a redirect from admin.php to admin/ but if possible I would like the usability of the first rule carried forward.
Cheers!
Adam
Organize your rules like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /?action=viewCategoryName&categoryName=$1&page_identifier=$2 [L,QSA,B]
RewriteRule ^([^/]+)/?$ /?action=viewHomepageName&page_identifier=$1 [L,QSA,B]
So I have shortened article.php?id=10 to article/10. And it all seemed to work fine. But little did I know that it ruined the rest of my URLs. So with http://localhost/forgot/, I'd have to go to http://localhost/forgot/index to actually reach it. Here's what I'm using
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/([0-9]+)$ article.php?id=$1
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I want to go to http://localhost/forgot/ instead of http://localhost/forgot/index/ Any ideas?
Have your rules like this:
Options -MultiViews
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/([0-9]+)$ article.php?id=$1 [L,QSA,NC]
# rewrite from /dir/file/ to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
I'm trying to replace question marks and equal signs so that I can use URLs like the following:
http://www.mydomain.com/categories/id/23/name/category-name
The above url would be internally redirected to
http://www.mydomain.com/categories?id=23&name=category-name
I am using this .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# external redirect from /view.php?id=1 to /view/id/1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php\?([^=]+)=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [L,R=301]
# internal forward from /view/id/1 to /view.php?id=1
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [L,QSA]
but it's not working as expected. When I go to http://www.mydomain.com/categories/id/23/name/category-name I get a 500 internal server error. Why? What am I doing wrong?
updated code
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?(.*)$ $1/$2=$3&$4 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)$ $1.php?$2 [L,QSA]
This rule should take care of recursively replacing each / with = and finally making it a nice Query String:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?(.*)$ $1/$2=$3&$4 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)$ $1.php?$2 [L,QSA]
You need to comment out or delete this rule:
# RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [L,QSA]
UPDATE: You .htaccess need re-ordering and little bit of tweaking. This should work:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /mydomain/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?(.*)$ $1/$2=$3&$4 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/(.+?)&?$ $1.php?$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
the code below works excellent however if I have a url such as /calgary/dashboard.php?step=change-password PHP won't recognize the $_GET['step']
What can I change here so that I can use $_GET['']
RewriteEngine On
RewriteBase /city_name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ /city_name/index.php?page=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /([^\./]+)\.php$
RewriteCond %{DOCUMENT_ROOT}/city_name/%1.php -f
RewriteRule ^(.*)/([^\./]+)\.php$ /city_name/$2.php?page=$1 [L]
Use QSA,
RewriteRule (.*)/$ /city_name/index.php?page=$1 [L,QSA]
RewriteRule ^(.*)/([^\./]+)\.php$ /city_name/$2.php?page=$1 [L,QSA]
Adding the query string flag (QSA) to the Rule should fix it, e.g:
RewriteRule ^(.*)/([^\./]+)\.php$ /city_name/$2.php?page=$1 [QSA,L]