0

I've been working on a new set of htaccess rewrite rules for a module I am creating. The htaccess file already has some rules for the PHP-based application and those rules are now broken. I am wondering if there is a way to fix this. Here is the code:

# My Module Rules
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*?)/?$ ./page.php?url=$1 [L]

RewriteCond %{THE_REQUEST} /page\.php\?url=([^\&\ ]+)
RewriteRule ^/?page\.php$ ./%1/? [R=301, L]
</IfModule>

# Application Rules
<IfModule mod_rewrite.c>
RewriteEngine on

# RewriteBase is set to "/" so rules do not need updating if the
# installation directory is relocated.  It is imperative that
# there is also a RewriteCond rule later that can effectively get
# the actual value by comparison against the request URI.
# 
# If there are _any_ other RewriteBase directives in this file,
# the last entry will take precedence!
RewriteBase /

# Redirect directories to an address with slash
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$  $1/ [R]

# Send all remaining (routable paths) through index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Determine and use the actual base
RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
RewriteRule ^.*$ %2index.php [QSA,L]
</IfModule>

The rules I have above are working for my module URLs:

https://www.example.com/page.php?url=test-page => https://www.example.com/test-page/

However, it's breaking the rewrite rules for the main application, here is an example:

https://www.example.com/admin/setup => 404 Error

I've tried to re-arrange the rules, but then my rewrite doesn't work. I'm also trying to not edit the application's rules because those are standard for people who use it.

I tried to add another conditional that would check to see if the requested URL contains "page.php", but it did not fix the issue. Here was the code I used:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/?page\.php$
RewriteRule ^/?(.*?)/?$ ./page.php?url=$1 [L]

RewriteCond %{THE_REQUEST} /page\.php\?url=([^\&\ ]+)
RewriteRule ^/?page\.php$ ./%1/? [R=301, L]
</IfModule>

Perhaps something is wrong with the code and I'm hoping someone could point me in the right location. Thank you all!

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
John Lewis
  • 11
  • 1
  • The [RewriteLog](https://stackoverflow.com/questions/9632852/how-to-debug-apache-mod-rewrite) can point you in the right direction. Ordering is important, FallbackResource or MultiViews are sometimes better options, and `[L]` and `[END]` flags do different things. Improve the comments, btw, and find a tutorial that doesn't still recommend ``. – mario Aug 19 '20 at 16:23
  • The pattern `^/?(.*?)/?$` matches `admin/setup` already, so now you’d be rewriting this to your own `page.php`. If that is not what you want - then you need to be more specific in what you actually want to begin with. // This all sounds kinda like the wrong approach to begin with. The application appears to use the usual pattern, of rewriting just _anything_ that does not exist to its index.php, and then handle everything else from there. There should probably be some sort of mechanism for your to handle your own module’s routing needs that same way. – CBroe Aug 20 '20 at 06:43
  • @CBroe Unfortunately, it's not that simple. The application I am using is encoded using ionCube so I cannot adjust or add rewrites. They also have not created a way to add your own rewrites like with WordPress. So I'm left to sort of make my own. The approach is not the best, but I'm trying to make the best of it. The "Application Rules" are set by the software, I cannot change those rules. So what I want to do is make my own rewrite rules, specific to page.php, that will not impact the other rewrites like admin/setup. Does that make sense? – John Lewis Aug 20 '20 at 15:23
  • You’ll still have to be more specific in your pattern then, which URLs exactly you want handled by your own script. It would probably make the most sense, if you used a static prefix as your own kind of “name space”, to avoid collisions with other URLs the system might be using. – CBroe Aug 21 '20 at 06:27
  • @CBroe, do you have any example of a static prefix? – John Lewis Aug 24 '20 at 16:22
  • Just something like `/my-module/test-page/` – CBroe Aug 25 '20 at 06:34

0 Answers0