2

I have a problem with the colons used in url

this my url

http://localhost/1:1

this my htaccess

RewriteEngine On
RewriteRule ^/(.*):(.*) index.php/$1:$2

this error show Forbidden You don't have permission to access /1:1 on this server.

# Virtual Hosts
#
<VirtualHost *:80>
  ServerName localhost
  ServerAlias localhost
  DocumentRoot "${INSTALL_DIR}/www"
  <Directory "${INSTALL_DIR}/www/">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride all
        Order Allow,Deny
        Allow from all
  </Directory>
</VirtualHost>

1 Answers1

1

There are a few issues here...

  1. In a per-directory .htaccess context, the URL-path matched by the RewriteRule pattern never starts with a slash, so the regex ^/(.*):(.*) will never match and the directive does nothing. So, the RewriteRule pattern would need to be ^(.*):(.*) - no slash prefix.

    • However, this regex is very general and is most probably matching too much. If you are expecting a request of the form /1:1, ie. /<number>:<number> then use a more specific regex, eg. ^\d+:\d+$
  2. Since you are getting a 403 Forbidden (as opposed to a 404 Not Found) I assume you are on a Windows server. The "problem" here is that : (colons) are not valid characters in Windows filenames. This is an issue with .htaccess because the request is mapped to the filesystem before .htaccess (and mod_rewrite) is processed - at which point the 403 is triggered. You would need to rewrite the request in the main server config (or VirtualHost container) instead - which occurs before the request is mapped to the filesystem.

So, what you are trying to do... rewrite the request that contains a colon, using .htaccess on a Windows server is not possible. You can do this on Linux (that permits colons in filenames) OR in the main server config (server or virtualhost context) on Windows, but not in .htaccess.

When using mod_rewrite in a server (or virtualhost) context (as opposed to .htaccess) you do need the slash prefix (on both the pattern and substitution strings). For example:

# In a "server" (or "virtualhost") context,
#   not ".htaccess" (or "<Directory>" section in the server config)

RewriteEngine On

# Internally rewrite "/1:1" to path-info on "index.php"
RewriteRule ^/\d+:\d+$ /index.php$0 [L]

The $0 backreference contains the entire URL-path that is captured by the RewriteRule pattern. This includes the slash prefix (when used in a server context), which is why the slash delimiter is omitted in the substitution string.


UPDATE:

I made the changes, please look at my question again and see, I entered it correctly

<VirtualHost *:80>
  ServerName localhost
  ServerAlias localhost
  DocumentRoot "${INSTALL_DIR}/www"
  <Directory "${INSTALL_DIR}/www/">
    Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride all
        Order Allow,Deny
        Allow from all
  </Directory>
</VirtualHost>

You don't seem to have made any changes; at least not in the right section? As mentioned above, these directives need to be added directly to the <VirtualHost> container (that you have posted). They cannot be added to the .htaccess file on a Windows OS - they simply won't do anything and you'll get the 403 Forbidden response as stated.

The above should be written like this:

<VirtualHost *:80>
  ServerName localhost
  ServerAlias localhost
  DocumentRoot "${INSTALL_DIR}/www"

  # Enable the rewrite engine in a virtualhost context
  RewriteEngine On

  # Internally rewrite "/1:1" to path-info on "index.php"
  RewriteRule ^/\d+:\d+$ /index.php$0 [L]

  <Directory "${INSTALL_DIR}/www/">
    Options -Indexes -Includes +FollowSymLinks -MultiViews
    AllowOverride all
    Require all granted
  </Directory>
</VirtualHost>

You will need to restart Apache for these changes to take effect. (Unlike .htaccess files that are interpreted at runtime.)

However, what other directives do you have in .htaccess and how are your other URLs being routed? You posted the following directive in comments:

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

This routes the URL quite differently from what you are requesting in your question. In your question you are passing the URL-path as additional path-info to index.php. However, in this directive you are passing the URL as part of the query string? How do these relate? Why are they different? You obviously need to pass the URL in the way your "MVC application" is expecting.

MrWhite
  • 11,643
  • 4
  • 25
  • 40
  • Thank you very much for posting and commenting on this guide for me, but this solution did not work either. Yes, I want this instruction for use in Vamp Server. On a Linux virtual host, if possible, I will try it once. Try – amir shariflou Jul 13 '21 at 03:24
  • Or rather, what instructions can I get from the colons in the parameters? Do you have a solution? this my error (Apache/2.4.37 (Win64) PHP/7.0.33 Server at localhost Port 80) – amir shariflou Jul 13 '21 at 03:30
  • The solution (workaround) I posted above should work as written. I've tested this on an Apache/Windows server using a `` container in the server config and it is working as intended... to pass the URL-path (`/1:1`) as PATH_INFO to the `/index.php` script (which appears to be what you were trying to do in your question). (This isn't the only way to pass the URL-path. You could pass it as a query string or not pass anything and examine `REQUEST_URI` instead - but this depends on what your script is expecting.) – MrWhite Jul 14 '21 at 00:22
  • @amirshariflou Where exactly are you using these directives? Please add the relevant server directives to your question. Did you restart Apache after changing the server config? "what instructions can I get from the colons in the parameters?" - Sorry, I'm not sure what you mean by this? Are you still getting a 403 Forbidden? How is your script (`index.php`) expecting to read the URL-path? "this my error" - I don't see an error? – MrWhite Jul 14 '21 at 00:27
  • @amirshariflou I've updated my answer to remove an additional/unnecessary slash in the _substitution_ string. Although this is really just a "tidy-up", it doesn't actually make any difference to it "working" or not. – MrWhite Jul 14 '21 at 00:30
  • Thank you very much for your time, but my problem was not solved. Please write the exact code for me to find out what it is. I tried many ways but it did not work, but the trap transfer to the Linux host runs there without the need for code. I do not know why it bothers in the wampserver - I even decided to use another xampp. But I know it has to be corrected with a code in the htaccess file – amir shariflou Jul 15 '21 at 10:11
  • See this address I want to do exactly that - http://www.recitequran.com/1:1 – amir shariflou Jul 15 '21 at 10:13
  • @amirshariflou "Please write the exact code for me" - the code in my answer _is_ the exact code. However, you need to place it in the correct place in your server config (not `.htaccess`) and restart your server. This depends on how you've configured your server. Please add the relevant sections of your server config to your question, with these directives in-place - I can then have a look and check. "I do not know why it bothers in the wampserver" - as stated in my answer, this is a limitation of Windows OS, not Apache. – MrWhite Jul 15 '21 at 22:06
  • @amirshariflou " I know it has to be corrected with a code in the htaccess file" - No. As I stated in my answer, you cannot use `.htaccess` for this (on Windows). You need to place these directives in the main server config (or `` container). – MrWhite Jul 15 '21 at 22:07
  • @amirshariflou What other directives do you have in your `.htaccess` file? How are other URLs being routed? – MrWhite Jul 15 '21 at 22:17
  • My routing is because I work with mvc php, and I work according to the standard, but the point here is what should happen in the htaccess file that when you enter the address, the wamp server does not make an error and returns the values 1: 1 as a parameter Try it on your system once, it's very important that it does – amir shariflou Jul 16 '21 at 11:33
  • Error Forbidden You don't have permission to access /1:1 on this server. Apache/2.4.37 (Win64) PHP/7.0.33 Server at localhost Port 80 – amir shariflou Jul 16 '21 at 11:35
  • RewriteRule ^(.+)$ index.php?url=$1 [QSA,L] – amir shariflou Jul 16 '21 at 11:35
  • Do you have access to the Apache server config? The `` container that _defines_ your site? Please edit your question to include this information. "the wamp server **does not** make an error" / "Error Forbidden You don't have permission to access"?! On which server are you getting the "Forbidden" response? – MrWhite Jul 16 '21 at 11:41
  • I made the changes, please look at my question again and see, I entered it correctly – amir shariflou Jul 16 '21 at 14:31
  • @amirshariflou I've updated my answer. However, we need to also see the contents of your `.htaccess` file as this might be conflicting and causing additional problems. – MrWhite Jul 17 '21 at 11:25