I'm strugglening with my .htaccess file in orther to achieve this:
a.com/male-items (OR)
a.com/male-items/popularity -> a.com/index.php?g=m&sort-popularity
a.com/female-items (OR)
a.com/female-items/popularity -> a.com/index.php?g=f&sort=popularity
a.com/male-items/alphabet -> a.com/index.php?g=m&sort=alphabet
a.com/male-items/alphabet/a -> a.com/index.php?g=m&sort=alphabet&l=a
(and same for female)
I know it should be something like
RewriteRule ^a$ a.com/index.php?q=$1
But actually looking into the different mod-rewrite / regex explanations and cheat-sheets doesn't help a lot with getting it to work. The hard part is to understand how do you define the different parametes in the address and then use them in the rewritten url.
(any explanations with your solution would be appretiated)
Use these rules in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^male-items/?$ /index.php?g=m&sort=popularity [L,QSA]
RewriteRule ^male-items/([^/]+)/?$ /index.php?g=m&sort=$2 [L,QSA]
RewriteRule ^male-items/([^/]+)/([^/]+)/?$ /index.php?g=m&sort=$2&l=$3 [L,QSA]
RewriteRule ^female-items/?$ /index.php?g=f&sort=popularity [L,QSA]
RewriteRule ^female-items/([^/]+)/?$ /index.php?g=f&sort=$2 [L,QSA]
RewriteRule ^female-items/([^/]+)/([^/]+)/?$ /index.php?g=f&sort=$2&l=$3 [L,QSA]
These htaccess lines redirect all nonexisting files/folders to index.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
inside index.php you can use: $_SERVER['REQUEST_URI'] to parse your parameters.
Related
[Edited code]*
This is the current code I am using
Options +FollowSymLinks
RewriteCond %{REQUEST_URI} (bus/(.*))
RewriteRule bus/(.*) page1.php?param=$1 [L,NC,QSA]
RewriteCond %{REQUEST_URI} (car/(.*))
RewriteRule car/(.*) test/page2.php?param=$1 [L,NC,QSA]
RewriteCond %{REQUEST_URI} (bike/(.*))
RewriteRule bike/(.*) test/page3.php?param=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/(.+)$ new.php?param1=$1¶m2=$2 [L,QSA]
I am trying to get the wild card condition in the last line if the above conditions fail. But the server returning with the 404. Its not catching the condition.
Any Help would be appreciated.
Keep all specific rules are top and 3rd generic catch-all rule at the last:
RewriteEngine On
RewriteRule ^bus/(.+)$ page1.php?param=$1 [L,NC,QSA]
RewriteRule ^car/(.+)$ test/page2.php?param=$1 [L,NC,QSA]
RewriteRule ^bike/(.+)$ test/page3.php?param=$1 [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/(.+)$ new.php?param1=$1¶m2=$2 [L,QSA]
Two RewriteCond are required before last rule to prevent rewriting actual files and directories.
Your need to use the L flag at the end of your rules to stop multiple rule processing :
RewriteRule ^ask/(.*) forum.php?param=$1 [L,NC]
RewriteRule ^city/(.*) city.php?param=$1 [L,NC]
I want to rewrite the following:
http://www.example.com/map to http://www.example.com/index.php?p=map
and
http://www.example.com/map/1 to http://www.example.com/index.php?p=map&id=1
My current code is:
RewriteEngine On
RewriteRule ^([^/]*)\$ /index.php?p=$1 [NC]
RewriteRule ^([^/]*)/([^/]*)\$ /index.php?p=$1&id=$2 [L]
But if fails...
Who can help me with the right .htaccess code? Thanx in advance!
You need to ignore real files and directories from your rewrite and don't escape $.
You can use:
RewriteEngine On
# rule to ignore files and directories from all rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ /index.php?p=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?p=$1&id=$2 [L,QSA]
So, I have been reading up on mod_rewrite because I want to remove the .php extension and have the URL look different, so that the end user doesn't see all the arguments in the URL.
My goal:
/foo.php?p=bar
Looks like:
/foo/bar
Where foo and bar can be anything, numeric and chars.
I'm only working with .php files, so it doesn't have to work with .html and alike.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [F]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^([^/]+)/?$ $1.php [L]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?p=$2 [L]
RewriteEngine On
Options +FollowSymLinks
RewriteBase /
RewriteRule ^api/people/([^/]*)$ api/people.php?id=$1
RewriteRule ^api/people/([^/]*)/([^/]*)$ api/people.php?action=$1&id=$2
In the above example, there are to RewriteRules one detects if a url is for example domain.com/api/people/1 and the other detects domain.com/api/people/settings/1
I usually get Error 500 when I try access the latter url, is there a way to sort out the HTACCESS so that it can detect if the url is either the first or last?
Keep your .htaccess like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^api/people/([^/]+)/?$ api/people.php?id=$1 [L,QSA]
RewriteRule ^api/people/([^/]+)/([^/]+)/?$ api/people.php?action=$1&id=$2 [L,QSA]
There are 4 rewrite rules, but only the first 2 work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^news/article/([^/]+)$ /path/to/news/?id=$1
RewriteRule ^news/page/([^/]+)$ /path/to/news/?page=$1
RewriteRule ^company/careers/id/([^/]+)$ /path/to/company/careers.php?id=$1
RewriteRule ^company/careers/page/([^/]+)$ /path/to/company/careers.php?page=$1
/path/to is always the same for all. The two news rules work perfectly, but the two company rules do not.
What's wrong here?
Edit to add (via comments):
This is how the 4 URLs look like.
http://domain.com/folder/folder2/news/article/1
=> translates correctly to http://domain.com/folder/folder2/news/?id=1
http://domain.com/folder/folder2/news/page/2
=> translates correctly to http://domain.com/folder/folder2/news/?page=2
http://domain.com/folder/folder2/company/careers/id/1
=> should translate to http://domain.com/folder/folder2/company/careers.php?id=1, but doesn't
http://domain.com/folder/folder2/company/careers/page/2
=> should translate to http://domain.com/folder/folder2/company/careers.php?page=2, but doesn't
The .htaccess file is located in http://domain.com/folder/folder2/.htaccess.
Change your .htaccess to this:
Options -MultiViews
RewriteEngine On
RewriteBase /folder/folder2/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^news/article/([^/]+)/?$ news/?id=$1 [L]
RewriteRule ^news/page/([^/]+)/?$ news/?page=$1 [L]
RewriteRule ^company/careers/id/([^/]+)/?$ company/careers.php?id=$1 [L]
RewriteRule ^company/careers/page/([^/]+)/?$ company/careers.php?page=$1 [L]