mod-rewrite index.php - apache

I have these URLs:
index.php?area=guilds
index.php?area=guilds&page=create
index.php?area=guilds&page=view&name=The+Unit
Could I have only 1 rewrite rule for this somehow?
/guilds
/guilds/create
/guilds/view/The+Unit

Try these rules:
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^[^/]+$ index.php?area=$0 [L]
RewriteRule ^([^/]+)/([^/]+)$ index.php?area=$1&page=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?area=$1&page=$2&name=$3 [L]
The first rule is to avoid that URLs are rewritten that already can be mapped to an existing file. The other rules reflect your three cases.

This one should work :
RewriteRule /(.+)(?:/(.+))?(?:/(.+))? index.php?area=$1&page=$2&name=$3

Related

RewriteRule in same .htaccess on two different files

I want to make a rewrite rule that will work on two different files which are contained in the same folder.
localhost/nameOfUser will be applied to index.php
localhost/nameOfUser?score=12 or localhost/nameOfUser/12 will be applied to post.php
I've managed to make the first one work with
RewriteEngine On
RewriteRule ^(css|fonts)($|/) - [L]
RewriteRule ^(php|avatars)($|/) - [L]
RewriteRule ^(user|post)($|/) - [L]
RewriteRule ^(.*)$ index.php?user=$1 [NC,QSA]
if I add
RewriteRule ^(.*)$ post.php?user=$1 [NC,QSA]
The second starts to work, but the first one doesn't.
Is it even possible to make rewrite for two different files in the same .htaccess file?
Give the following rules a try:
RewriteEngine On
RewriteRule ^(css|fonts|php|avatars|user|post)($|/) - [L]
RewriteRule ^([^/]+)$ index.php?user=$1 [NC,QSA,L]
RewriteRule ^([^/]+)/([^/]+)$ post.php?user=$1&score=$2 [NC,QSA,L]
However, if you want nameOfUser?score=12 to work, you'll need to modify them to:
RewriteEngine On
RewriteRule ^(css|fonts|php|avatars|user|post)($|/) - [L]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)$ /index.php?user=$1 [NC,QSA,L]
RewriteCond %{QUERY_STRING} ^score=(.+)$
RewriteRule ^([^/]+) /post.php?user=$1&score=%1 [NC,L]

htaccess multipurpose rewrite rules conflict

I have 2 simple PHP files:
product.php
product-options.php
And in my htaccess file i have:
RewriteEngine On
RewriteRule ^product$ product.php [L]
RewriteRule ^options$ product-options.php [L]
RewriteRule ^product-([^/]+)/?$ product.php?id=$1 [L]
RewriteRule ^options-([^/]+)/?$ product-options.php?id=$1 [L]
Everything works fine when i call "mysite.com/product" or "mysite.com/product-MYID"
But when i call "mysite.com/options" or "mysite.com/option-MYID" i see the correct url but the content of the file is "product" / "product-MYID" !!! Why???
PS: i need that both files are accessibile with and without the GET ?id=ID
Or you can use:
RewriteEngine On
RewriteRule ^product$ product.php [L]
RewriteRule ^options$ product-options.php [L]
RewriteRule ^product-([^/\.]+)/?$ product.php?id=$1 [L]
RewriteRule ^options-([^/\.]+)/?$ product-options.php?id=$1 [L]
because, I think, you don't use . in your product-... or option-...
That is because your rules are executing 2nd time as regex is:
^product-([^/]+)/?$
Which matches /product-MYID URI as well as rewritten URI: product-options.php
In order to avoid this you can use these rules:
RewriteEngine On
# If the request is for a valid directory or file then skip rewrite rules
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^product/?$ product.php [L]
RewriteRule ^options/?$ product-options.php [L]
RewriteRule ^product-([^/]+)/?$ product.php?id=$1 [L,QSA]
RewriteRule ^options-([^/]+)/?$ product-options.php?id=$1 [L,QSA]

Apache 2.2 mod rewrite loop. How to replace [END]

RewriteEngine on
I have following code:
RewriteCond %{REQUEST_URL} ^$
RewriteRule ^files/(.*)$ getfile.php?file=$1 [L]
RewriteRule ^(.+)/index([0-9]*).html$ index.php?path=$1/&page=$2 [QSA]
RewriteRule ^(.+).html$ index.php?path=$1.html [QSA]
RewriteRule ^(.+)/(.+).htm$ index.php?path=$1/&article=$2.htm [QSA]
RewriteRule ^(.+/)$ index.php?path=$1 [QSA]
RewriteRule ^(.+)$ index.php?path=$1 [QSA]
This code translate urls to parameter path and article according to situation. All good and bad url request is routed to index.php except for files within folder files/.
I have problem with last line of code. It is primarily for all bad urls (all other is processed earlier). With this rule it start to loop over and over again, because all of url match this rule. This rule have to match once or never, but without [END] I cannot contrive. I have older version of apache (2.2) so I cannot use this direction.
Solution for this I have tried is something like this:
RewriteCond %{QUERY_STRING} !^.*path=([^&]*).*$
RewriteCond %{REQUEST_URI} !^/((files|design)/.+)$
RewriteRule ^(.+)$ index.php?path=$1 [QSA]
but cannot succeed.
Try adding a L flag to the end of each rule and add a redirect environment check in the beginning to pass through any previously internally redirected URI:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteRule ^files/(.*)$ getfile.php?file=$1 [L]
RewriteRule ^(.+)/index([0-9]*).html$ index.php?path=$1/&page=$2 [L,QSA]
RewriteRule ^(.+).html$ index.php?path=$1.html [L,QSA]
RewriteRule ^(.+)/(.+).htm$ index.php?path=$1/&article=$2.htm [L,QSA]
RewriteRule ^(.+/)$ index.php?path=$1 [L,QSA]
RewriteRule ^(.+)$ index.php?path=$1 [L,QSA]

RewriteRule Last flag being ignored

I don't get that if a rewrite rule matches and it has the Last [L] flag that it still executes the rules beneath. I redirect all calls to the public folder but I've added an exception for images, but the exception is being ignored. I thought that with the [L] flag if a rule matches it stops looking for rules beneath.
This is my .htaccess file:
RewriteRule ^image/(.*)/?$ image.php?t=$1 [L]
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
If I remove the bottom 2 rules it does work.
Thanks in advance
It is not ignored.
It is the specific of .htaccess.
It reruns every time URL is changed.
Try this instead.
RewriteEngine on
RewriteCond %{QUERY_STRING} rewritten
RewriteRule .* - [L]
RewriteRule ^image/(.*)/?$ image.php?t=$1&rewritten=1 [L,QSA]
RewriteRule ^$ public/?rewritten=1 [L,QSA]
RewriteRule (.*) public/$1?rewritten=1 [L,QSA]

Rewrite rule not working

I want to write a rule that redirects all URLs of a certain pattern to a PHP file. Exception: the control/ directory where the CMS resides.
Why does
RewriteCond %{REQUEST_URI} ^/([^control]+)/([^/]+)$
RewriteRule .* /pages/index.php?language=%1&page=%2&%{QUERY_STRING} [L]
not work for
domain.com/deutsch/start
(it throws a 404), while
RewriteCond %{REQUEST_URI} ^/([^control]+)/([^/]+)/debug$
RewriteRule .* /pages/index.php?language=%1&page=%2&page_debug=yes&%{QUERY_STRING} [L]
works for
domain.com/deutsch/start/debug
?
You need to use two conditions:
RewriteCond %{REQUEST_URI} !^/control/
RewriteCond %{REQUEST_URI} ^/([^/]+)/([^/]+)$
RewriteRule .* /pages/index.php?language=%1&page=%2&%{QUERY_STRING} [L]
But you should use your pattern directly in the RewriteRule directive if possible:
RewriteCond $1 !=control
RewriteRule ^([^/]+)/([^/]+)$ /pages/index.php?language=$1&page=$2&%{QUERY_STRING} [L]
Another hint is to use the QSA flag instead of appending the query explicitly:
RewriteCond $1 !=control
RewriteRule ^([^/]+)/([^/]+)$ /pages/index.php?language=$1&page=$2 [L,QSA]