Search code examples
apache.htaccessmod-rewriteurl-rewritingurl-shortener

Apache RewriteRule doesn't apply


I'm trying to create a URL shortner for my site, which any request to example.com/u/* should be rewritten to example.com/cpp-cgi-test.cgi?* !

Note that: the directory /u doesn't exist and I added it to URL only to prettify the short link!

I'm on a cloud hosting provider environment with Cpanel; not a server!


Content of .htaccess under public_html:

Options +FollowSymLinks
RewriteEngine On
RewriteRule "^/u/(.*)" "/cpp-cgi-test.cgi?$1" [NC]

# BEGIN cPanel-generated php ini directives, do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor)
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI)
<IfModule php7_module>
   php_flag display_errors On
   php_value max_execution_time 30
   php_value max_input_time 60
   php_value max_input_vars 1000
   php_value memory_limit 128M
   php_value post_max_size 8M
   php_value session.gc_maxlifetime 1440
   php_value session.save_path "/var/cpanel/php/sessions/ea-php73"
   php_value upload_max_filesize 2M
   php_flag zlib.output_compression Off
</IfModule>
<IfModule lsapi_module>
   php_flag display_errors On
   php_value max_execution_time 30
   php_value max_input_time 60
   php_value max_input_vars 1000
   php_value memory_limit 128M
   php_value post_max_size 8M
   php_value session.gc_maxlifetime 1440
   php_value session.save_path "/var/cpanel/php/sessions/ea-php73"
   php_value upload_max_filesize 2M
   php_flag zlib.output_compression Off
</IfModule>
# END cPanel-generated php ini directives, do not edit

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php72” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php72___lsphp .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

Where only three first lines added by me!


Problem is:

When I'm trying to access example.com/u/Abcde it ends up with a error 404!

Also, I tried the code below only in case of that the problem because of the fact /u directory doesn't exist; but with this code when I enter example.com/cpp-cgi-test.cgi still I'm not going to index.html:

Options +FollowSymLinks
RewriteEngine On
RewriteRule "^/cpp-cgi-test.cgi" "https://example.com" [NC]

Solution

  • Based on your shown samples, could you please try following Rules in your .htaccess file, considering that any URI which starts from u|U should be re-written to cpp-cgi-test.cgi file in backend with a query string passed to it. Also please make sure you clear your browser cache before you test your URLs.

    RewriteEngine ON
    RewriteRule ^u/(.*)/?$ cpp-cgi-test.cgi?$1 [NC,L]