Apache URL Rewriting issue - apache

i've set the rules,
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ user.php?userid=$1
it redirects http://localhost/username to http://localhost/user.php?id=username works fine,
but for other existing folder page still redirects to user.php page; not that existing folder, ex http://localhost/folder it still redirects to http://localhost/folder/?userid=folder,
how can it make it to work for existing directories, ex when i type http://localhost/folder then page should shown of /folder/ directory ???

Replace your .htaccess code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^(messages)/?$ index.php?page=$1 [L,NC,QSA]
RewriteRule ^(A-Z0-9_-]+)/?$ user.php?userid=$1 [L,NC,QSA]
If it still doesn't work please post matching long entries from access.log and enable RewriteLog in your httpd.conf and post logs from there in your question.

The problem is that your second RewriteRule has no RewriteConds. Your two RewriteConds only apply for the first rule, then they stop. The second rule therefore applies unconditionally to every request URI.
So this part of your .htaccess should look like this instead:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^messages/?$ index.php?page=messages [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/?$ user.php?userid=$1 [L,NC,QSA]

Related

url rewriting synthax with .htaccess

I am on apache 2 and I would know if my htacces is correct
my url are for example :
localhost/test/boutique/index.php
localhost/test/boutique/index.php/language,en
localhost/test/boutique/index.php/Products/Description/products_id,1
localhost/test/boutique/index.php/Products/Description/products_id,2/language,fr
What is the best approach for a good url like above
In suppose index.php must deseapear to hav someting like that
localhost/test/boutique/Products/Description/products_id,1
I try this but it does'nt work
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://localhost/test/boutique/index.php/$1 [PT,L]
Assuming /test/boutique/ is a real directory, you can use these rules inside /test/boutique/.htaccess:
RewriteEngine On
RewriteBase /test/boutique/
# remove index.php if entered directly by clients
RewriteCond %{THE_REQUEST} /index\.php/(\S*)\s [NC]
RewriteRule ^ %1 [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!index\.php$).* index.php/$0 [L,NC]

Use .htaccess to cease processing if particular string is captured in URL

I need the following .htaccess file to redirect requests for pages ending in njiswebhook to \webhooklanding.php.
At the moment the requests are still being passed to index.php and are being routed by Slim. How can I tell .htaccess to cease processing if a particular condition is met?
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*njiswebhook /webhooklanding.php [QSA,L]
RewriteRule ^ index.php [QSA,L]
Make an exception from last rule , otherwise nothing will pass to webhooklanding.php like this :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*njiswebhook webhooklanding.php [QSA,L]
RewriteRule !^webhooklanding\.php index.php [QSA,L]
The [END] flag stopped processing upon reaching that clause.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*njiswebhook /webhooklanding.php [QSA,L,END]
RewriteRule ^ index.php [QSA,L]
The above solution has been implemented and tested.

multiple RewriteRules in htaccess file, the first one over writes the second one?

I am trying to run multiple RewriteRules in my htaccess file.
however, the first RewriteRule, overwrites the second RewriteRule for some reason!
this is what i have in my htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^([a-zA-Z0-9-/]+).html$ items.php?itemsurl=$1 [L]
RewriteRule ^([a-zA-Z0-9-/]+).html/$ items.php?itemsurl=$1 [L]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^([a-zA-Z0-9-/]+).html$ blog.php?blogurl=$1 [L]
RewriteRule ^([a-zA-Z0-9-/]+).html/$ blog.php?blogurl=$1 [L]
the first RewriteRule works fine but if I click on the links for blog.php?blogurl= it will simply take me to the items.php!
I could put the RewriteRules for the blog.php at the top of the htaccess file and put the RewriteRules for items.php bellow it and it will make the blog.php rewriterule work for blog.php but it will make the rewriterules for the items.php stop working and everything will point to the blog.php page!
So basically, only the first RewriteRule in the htaccess file works and it will overwrite the second one somehow.
could someone advise on this please?
Replace your code with this one:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/]+)\.html\/?$ items.php?itemsurl=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/]+)\.html\/?$ blog.php?blogurl=$1 [L]
RewriteCond affects only to the first RewriteRule.
The apache variables should be in these brakes: {}, not these ()
And one more thing - to check . you should escape it so: \.
As discussed in comments, you can use following rules:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^items/([\w-]+)\.html\/?$ items.php?itemsurl=$1 [L,QSA,NC]
RewriteRule ^blog/([\w-]+)\.html\/?$ blog.php?itemsurl=$1 [L,QSA,NC]

Order of rules in .htaccess file

I have an .htaccess file like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^foo.*$ /cgi/foo.cgi [L]
RewriteRule ^.*$ /cgi/fallback.cgi [L]
but when I go to a URL starting with foo in that folder, the browser still gets redirected to the fallback.cgi script. If I remove the second rule, the 'foo' line works OK.
According to my understanding, the first rule should take precedence, and the [L] should prevent any other rules from happening.
You are right, the 1st rule should be applied first and I think it is. The problem might be the 2nd rule is also being applied, so the code should be like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^foo.*$ /cgi/foo.cgi [L,NC]
RewriteCond %{REQUEST_URI} !/cgi/(foo|fallback)\.cgi [NC]
RewriteCond %{REQUEST_URI} !/foo [NC]
RewriteRule ^.*$ /cgi/fallback.cgi [L]
The ^ means beginning of string.
RewriteRule ^foo.*$ /cgi/foo.cgi [L]
So that rules ONLY matches /foo followed by zero or more characters.
To match a file beginning with "foo" use this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (^|/)foo.*$ /cgi/foo.cgi [L]
It matches:
/foo
/foo.php
/food.php
/foolish/dog
It does not match:
/kung-foo.cat
/FOO.php
Update
Remember that some browsers do redirect caching. I was testing this on my own server and had performed a redirect that was cached. Made me confused for a bit when my new rules weren't working.
It is applying 2nd rule because;
In 2nd rule your are matching .* (means everything)
RewriteCond lines are only being applied to 1st rule only
Correct code would be:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# don't do anything for a file, dir or symlnk
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ - [L]
RewriteRule (^|/)foo /cgi/foo.cgi [L,NC]
RewriteRule ^ /cgi/fallback.cgi [L]

htaccess rewrite rule for backend/frontend app

I have a yii app that has separate back end and front end. I am trying to make friendly url.
I have tried some online tools but could not get it correct
So I want this url
http://mydomain.lc/abs/admin/some/crazy/urls (for example
some/crazy/urls can be admin/index)
will be rewritten to:
http://mydomain.lc/abs/backend.php/some/crazy/urls (because this
url works directly)
And I have also front end site but they are both in the same project so they share .httacess.
the rule for .htaccess for front end is:
this url:
http://mydomain.lc/abs/some/crazy/urls (some can not be admin
here, so we can differentiate btw fron and back end)
should be
http://mydomain.lc/abs/index.php/some/crazy/urls
I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^abs/admin/?(.*?)$ abs/backend.php?url=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!abs/admin\/)(.*?)$ abs/index.php?url=$1 [QSA,L]
</IfModule>
the above script is not working.
Root is under abs folder and .htaccess in the root
You do not have to change anything except RewriteBase / to RewriteBase /abs
RewriteEngine On
RewriteBase /abs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^admin/?(.*?)$ backend.php?url=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!admin\/)(.*?)$ index.php?url=$1 [QSA,L]
I guess the problem is with the RewriteBase.
You may try this in the .htaccess file in /abs directory:
Update according to last OP comment: this one is correct: backend.php/some/crazy/urls
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /abs
# Backend admin
RewriteCond %{REQUEST_URI} !backend\.php [NC]
RewriteRule ^admin(.*) /backend.php/$1 [QSA,L,NC]
# Frontend
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteCond %{REQUEST_URI} !admin [NC]
RewriteRule ^(.*) /index.php/$1 [QSA,L,NC]