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]
Related
I have two different URLs:
1.: mysite.com/file.php
2.: mysite.com/**articles**.php?article=**something**
I would like to rewrite mysite.com/file.php to mysite.com/file.
AND
I would like to rewrite mysite.com/**articles**.php?article=something to mysite.com/**news**/**something**
I tried this but it's not working.
It rewrite mysite.com/file.php to mysite.com/file, but does not rewrite mysite.com/**articles**.php?article=**something** to mysite.com/**news**/**something**
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^?\ ]+)\.php
RewriteCond %{REQUEST_FILENAME} !exception.php
RewriteRule ^news/([^/]+)(/*)$ /articles.php?article=$1
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
(There is one exception in the code: exception.php which I don't want to rewrite.)
Can you please correct my code? I've been working on this for 4 days, I've read everything, but I can't do it.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect for file.php file.
RewriteCond %{REQUEST_URI} !/?exception\.php [NC]
RewriteCond %{THE_REQUEST} \s/(file)\.php/?\s [NC]
RewriteRule ^ /%1? [R=301,L]
##Internal rewrite for php files.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ $1.php [QSA,L]
##external + internal rewrite rules for articile.php here.
RewriteCond %{THE_REQUEST} \s/articles\.php\?article=(\S+)\s [NC]
RewriteRule ^ /news/%1? [R=301,L]
RewriteRule ^news/([^/]*)$ articles.php?article=$1 [NC,QSA,L]
How can I manage 3 subdirectories levels on Htaccess?
Explanation:
http://example.com/category1/
http://example.com/category2/
http://example.com/category3/
RewriteRule to *category.php?category=$1*
http://example.com/category1/type1/
http://example.com/category2/type7/
http://example.com/category3/type8/
RewriteRule to *type.php?category=$1&type=$2*
http://example.com/category1/type1/company3/
http://example.com/category2/type7/company4/
http://example.com/category3/type8/company9/
RewriteRule to *company.php?category=$1&type=$2&company=$3*
.htaccess file is located in initial root folder (where index.php)
Is the next the best way?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)(?:/)?$ /category.php?category=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)(?:/)?$ /type.php?category=$1&type=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)(?:/.*)?$ /company.php?category=$1&type=$2&company=$3 [L]
You are fairly close. Just note that RewriteCond is applicable to next immediate RewriteRule only so you check of !-f will only be used for first rule. You can have a separate rule to discard all requests that are for existing files and directories.
RewriteEngine On
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([\w-]+)/?$ category.php?category=$1 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ type.php?category=$1&type=$2 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ company.php?category=$1&type=$2&company=$3 [L,QSA]
[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 am using
RewriteRule ^(.*)/?$ index.php?page=a&i=$1
RewriteRule ^b/(.*)/?$ index.php?page=b&i=$1
to redirect urls of the form mysite/something to index.php?page=a&i=something and mysite/b/something to index.php?page=b&i=something. However, both urls are redirecting index.php?page=a&i=something. Why is this happening?
^[^\/]+\/([^\/]+)$ index.php?page=a&i=$1
^[^\/]+\/b\/([^\/]+)$ index.php?page=b&i=$1
.* in first rule will match everything.
To prevent this behavior you can use:
RewriteEngine On
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^b/(.+?)/?$ index.php?page=b&i=$1 [L,NC,QSA]
RewriteRule ^(.+?)/?$ index.php?page=a&i=$1 [L,QSA]
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.