Score:2

used colons for url htaccess http://localhost/1:1

cn flag

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>
Michael Hampton avatar
cz flag
Check the Apache error log.
Score:1
kz flag

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.

amir shariflou avatar
cn flag
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 avatar
cn flag
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)
kz flag
The solution (workaround) I posted above should work as written. I've tested this on an Apache/Windows server using a `<VirtualHost>` 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.)
kz flag
@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?
kz flag
@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.
amir shariflou avatar
cn flag
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 avatar
cn flag
See this address I want to do exactly that - http://www.recitequran.com/1:1
kz flag
@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.
kz flag
@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 `<VirtualHost>` container).
kz flag
@amirshariflou What other directives do you have in your `.htaccess` file? How are other URLs being routed?
amir shariflou avatar
cn flag
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 avatar
cn flag
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 avatar
cn flag
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
kz flag
Do you have access to the Apache server config? The `<VirtualHost>` 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?
amir shariflou avatar
cn flag
I made the changes, please look at my question again and see, I entered it correctly
kz flag
@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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.