I have an .htaccess file that looks like the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/(\d+?)/?$ index.php?cmd=$1&num=$2 [L]
RewriteRule ^(.+?)/?$ index.php?cmd=$1 [L]
</IfModule>
Basically, I'm trying to write an API that can handle requests like get_all/ or get/123/.
The problem is, when I try to go to either of the above URLs, the cmd parameter is always set to index.php and the num parameter is not set at all.
If I comment out either of the RewriteRule lines, then requests work for the remaining, uncommented out RewriteRule, but I need to be able to handle both cases.
I am aware of the looping that occurs with mod_rewrite, but in this case, I have no clue how to stop it. I can't even understand why the above rules are causing cmd to be set to index.php.
Can anyone please explain what is happening here and how to fix it?
Thank you.
Rules are behaving because RewriteCond is only applied to very next RewriteRule. Due to this your last RewriteRule is running without conditions and hence running twice by mod_rewrite loop.
Use this code to fix it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/(\d+?)/?$ index.php?cmd=$1&num=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?cmd=$1 [L]
</IfModule>
If you want to avoid repetition of Rewriteond then use:
<IfModule mod_rewrite.c>
RewriteEngine On
# skips files and directories from rewrite rules
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^(.+?)/(\d+?)/?$ index.php?cmd=$1&num=$2 [L]
RewriteRule ^(.+?)/?$ index.php?cmd=$1 [L]
</IfModule>
Related
So after a lot of testing - I've started to figure out my problems but still can't get the rewrite to work.
This is how my htaccess file looks now.
RewriteEngine On
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://abfielder.com/$1 [R=301,L]
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
Rule 1 is the one I'm having problems with
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
My understanding is now if I type this url into my browser
https://abfielder.com/schemdetail/id/158
I should get
https://abfielder.com/schematicdetails?id=158
The test of this here
https://htaccess.madewithlove.com/
Tells me my rules are ok.
And rules 2 and 3 are working fine.
However when I try and access
https://abfielder.com/schemdetail/id/158
I essentially get the page not found error.
Ok think I've finally fixed this for anyone else who is having issues with mod rewrite where they wish to have a rule to remove the file extension and other rewrites working together the order matters. Lastly if you're on a shared host type thing you may also need to set the rewrite base. This is how my htaccess file looks now.
RewriteEngine On
RewriteBase /
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://abfielder.com/$1 [R=301,L]
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
This allows me to have url's without the .php extension.
It also allows for
schemdetail/id/100 to be translated into schematicdetails?id=100
I'm trying to rewrite the url of a certain php file.
Aim for url: www.mysite.com/filename/topic/ or www.mysite.com/filename/topic
What works: www.mysite.com/filename and www.mysite.com/topic
I've tried all rewrite rules I know which should work, but every time I add the topic to the url I end up with a 500 internal server error
Current .htacces file
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
//No need for php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
//Rewriting the url
RewriteRule ^([a-zA-Z0-9_-]+)$ filename.php?topic=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ filename.php?topic=$1
You have conflicting rules and there is probably an issue with content negotiation with MultiViews. Also you aren't using all the flags ([L]) need to prevent continuation of rules matching.
You can't do it that way with the second set of rules and you don't need 2 rules just to have a / or not. You can make it optional. You need to do something like this.
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?topic=$2 [L]
</IfModule>
I have a htaccess file that rewrites the link
http://www.example.com/profiles.php?view_user=test
to
http://www.example.com/profiles?view_user=test
but i want to rewrite the remaining get variable --
profiles?view_user=test
to
profiles/view_user/test
help me out i dont know much of htaccess
Try adding these rules to the appropriate place in the htaccess file in your document root:
RewriteCond %{THE_REQUEST} \ /+profiles\.php\?([^=&]+)=([^&\ ]+)
RewriteRule ^ /profiles/%1/%2? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profiles/([^/]+)/([^/]+)/?$ /profiles.php?$1=$2 [L,QSA]
Also make sure you have multiviews turned off:
Options -Multiviews
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(profiles)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [QSA,L,NC]
I have some problem to build my url rewrite. On my local server, working with nginx, everything is going well. But as soon as I try to make it working for apache with htacces, I kind of get crazy.
My urls are looking like this:
http://example.com/controller/action/param1/value1/param2/value2
Following is the htaccess working for the previous case:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/(images|css|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?$1 [L]
</IfModule>
But in some case I have some extra parameter, for example for Facebook authentication:
http://example.com/controller/action?hauth.start=Facebook&hauth.time=1382665465
This case is not working with the previous htaccess. Nothing is going in the GET variables. How could I fix this?
Use QSA (Query String Append) flag in your rule
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/(images|css|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
</IfModule>
QSA flag makes sure to preserve an existing query string while adding new query parameters.
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]