.htaccess rewriteRule conflict - php

I am writing a code and build an htaccess file which has the following contents:
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?id=$1 [NC,L] # Handle page requests
RewriteRule ^category/([A-Za-z0-9-]+)/?$ category.php?cat=$1 [NC,L] # Handle category requests
RewriteRule ^author/([A-Za-z0-9-]+)/?$ author.php?auth=$1 [NC,L] # Handle author requests
The first rule works fine but the latter seems having conflict with the first rule. Everytime I access a url at http://example.com/category/foobar or http://example.com/author/fooauthor/ I always get a message that the page cant be found because it is still trying to open the index file.
Is there any possibility to get around with this?

You can set general rule in the last line:
RewriteEngine on
RewriteRule ^category/([A-Za-z0-9-]+)/?$ category.php?cat=$1 [NC,L]
RewriteRule ^author/([A-Za-z0-9-]+)/?$ author.php?auth=$1 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?id=$1 [NC,L]
This issue happened because first line support all urls

Related

Redirect with htaccess from index.php to another.php file

I want to create pretty url. But, I got some problem with .htaccess. For example I have url domain/some.php?f=query-string.
I want to change domain/query-string (expected url). Is that possible to change / redirect via .htaccess. Or maybe from php file itsself.
this is a bit of htaccess snippet i made, but i get it blank/error page
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteRule ^/([^/.]+)$ some.php?f=$1 [NC,L]
</IfModule>
Thanks for your attention.
RewriteRule ^/([^/.]+)$ some.php?f=$1 [NC,L]
In .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash, so the above will never match and it will do nothing. This should be written like the following instead:
RewriteRule ^([^/.]+)$ some.php?f=$1 [L]
The NC flag is not required here, since the regex is already "case-insensitive".

Htaccess two rules conflicting when trying to remove .php and create pretty URL

I am trying to remove the .php extension at the same time as creating a "pretty URL" for a certain page.
I am trying to turn domain.com/dox?id=3 into domain.com/3
Current rewrite conditions in my htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]*)\$ /dox?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
This creates an error at domain.com/3 that says:
Not Found
The requested URL /3.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
However if I add any extension to the code, it works. To explain what I mean, if I change the fourth line to this:
RewriteRule ^([^/]*)\.extension$ /dox?id=$1 [L]
Then the page domain.com/dox?id=3 will be accessible at domain.com/3.extension
Obviously the problem is the two rules are conflicting, but not being a htaccess whizz, I can't figure it out
Try this:
RewriteRule ^([A-Za-z0-9-]+)/?$ /dox?id=$1 [L]
Or if id is just a number then this would be more appropriate:
RewriteRule ^([0-9]+)/?$ /dox?id=$1 [L]

htaccess rewrite index.php to .html

I have following rewriterules:
RewriteEngine On
RewriteRule ^([^\-]*)-(.*)-von-(.*)\.html$ $1index.php?filter=$2&marke=$3 [QSA]
RewriteRule ^([^\-]*)-von-(.*)\.html$ $1index.php?marke=$2 [QSA]
RewriteRule ^([^\-]*)-(.*)\.html$ $1index.php?filter=$2 [QSA]
RewriteRule ^(.*)\.html$ $1index.php
The first 3 rules are working, but the fourth, just rewrite index.php to .html is not. What is wrong here?
EDIT:
The URL is example.com/folder/subfolder/index.php
In the folder I got following htaccess:
RewriteEngine on
RewriteRule ^subfolder(.*) subfolder/$1
And the htaccess in subfolder is the one above
Now the URL for the first rule is example.com/folder/subfolder-value1-von-value2.html and works, for the second and third rule it's example.com/folder/subfolder-value1.html and example.com/folder/subfolder-von-value2.html
So with logic the fourth rule should also work just without the parameters but it's not working
Place this in /folder/.htaccess:
RewriteEngine on
RewriteRule ^subfolder\.html$ subfolder/index.php [L,NC]
RewriteRule ^subfolder(.+) subfolder/$1 [L,NC]
You shouldn't add $1 to the second part of the rule.it'll append the first matching group to the index.php file name. what you are currently getting is 'indexindex.php'
if you just want to rewrite index.html to index.php then you can place following line at the end of the file.
RewriteRule ^index.html$ index.php [L,NC]
also you might wanna remove $1 part from other lines as well.
Can you try this & see ?
RewriteEngine on
RewriteRule ^(.*)\.html $1\.php

mod_rewrite not passing query string

I have a problem with mod_rewrite.
here is my .htaccess file:
#REWRITE
RewriteEngine On #Turn on the RewriteEngine
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^admin/(.*)/?$ admin.php?kappa=$1 [NC,L,QSA] # Handle Admin Panel
RewriteRule ^buypoint/([0-9]+)/?$ baltopoints.php?sid=$1 [NC,L] # Handle bit->Point requests
RewriteRule ^history/([0-9]+)?/?$ history.php?page=$1 [NC,L] # Handle Transaction requests
RewriteRule ^topupstatus/(.*)/?$ topupsta.php?ec=$1 [NC,L] # Handle index
RewriteRule ^refresh/(.*)?$ refresh.php [NC,L] # Handle Refresh requests
RewriteRule ^refill/(.*)?$ topup.php [NC,L] # Refill
RewriteRule ^topup/(.*)?$ topup.php [NC,L] # Refill
RewriteRule ^ucp/?(.*)?$ main.php [NC,L] # Handle index
RewriteRule ^logout/?(.*)?$ logout.php [NC,L] # Handle Logout
the rewrite only work for rule:
^buypoint/([0-9]+)/?$ baltopoints.php?sid=$1 [NC,L] rule
( buypoint/1 will rewrite to baltopoints.php?sid=1 )
otherwise, only work for first slashes (admin/viewbtx will rewrite to admin.php [with no query string])
Can someone help me about this problem?
The rule matches the regexp against the URi less the query string. A ? in the pattern makes the previous atom optional. This is std regexp syntax so ^a.php?e=$1$ will match a.phe=23 for example. You parse the query string using RewriteCond statement preceding the RewriteRule, for example:
RewriteCond %{QUERY_STRING} ^ec=(\d+)
and now %1 index number is available to the rules replacement string. Read up the examples in Apache Module mod_rewrite documentation.
I've figured it out by renaming admin.php to admincp.php and use RewriteRule ^admin/(.*)$ admincp.php?do=$1 [NC,L,QSA]rule instead of the one in question so i think it's apache bug or something
Same answer I gave to another question, it may be useful to you
How to htaccess redirect this long URL?

HTACCESS rewrite with get variables

I have a sub directory which has a bunch of .php files in it. I currently have this as my .HTACCESS to rewrite the urls cleanly.
RewriteEngine on
Options +FollowSymLinks
RewriteBase /mailing_list
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
This is fine because it turns http://www.example.com/mailing_list/home to look at http://www.example.com/mailing_list/home.php
What I also need to account for is a page using clean urls for the get variables. That page is called w.php and it passes ?i=(int)
When I try to access http://www.example.com/mailing_list/w/1 I get a 505 server error. What do I need to add to my htaccess to account for this? Is there a full proof way which will account for others I run in to. For example:
What if a page comes up called blogs and it passes the ID of the blog like this: http://www.example.com/mailing_list/blog/1
Will I have to add a new Rule for that page specifically?
Replace
RewriteRule ^(.*)$ $1.php
by
RewriteRule ^([^/]+)$ $1.php [L,QSA]
RewriteRule ^([^/]+)/(.*)$ $1.php?i=$2 [L,QSA]

Categories