skip to Main Content

I have read ALOT of threads about htaccess rewriting but struggling to get anything to work as I need it to.

I am spinning my wheels here for a few hours.

I have two pages in my folder.

/proposal/index.php
/proposal/dl.php

So normal queries would look like this.

/proposal/?code=123abc
/proposal/dl.php?code=123abc.

But I want to send it to the user like this:

/proposal/123abc/
/proposal/dl/123abc/

Can someone help me with this?

TIA

NOTE
I did try the code below which works perfectly on index.php but has no effect on dl.php (if I add dl.php to the mix)

RewriteEngine On
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]

2

Answers


  1. Chosen as BEST ANSWER

    In the end, this worked.

    RewriteEngine On
    RewriteRule ^dl/(.*) dl.php/$1 [L]
    RewriteRule ^index.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule . index.php [L]
    

    Now I am able to call the pages like this:

    /proposal/123abc/
    /proposal/dl/123abc/

    All I did was add the line below to my original code.

    RewriteRule ^dl/(.*) dl.php/$1 [L]
    

    The structure of my subdirectory is as follows:

    /proposal/.htaccess
    /proposal/index.php
    /proposal/dl.php

    Now I am able to provide nice URLs to the clients for these two files and call up their content using my parameter like:

    /proposal/123abc
    /proposal/dl/123abc

    Which would be like this in a normal way

    /proposal/index.php?code=123abc or /proposal/?code=123abc
    /proposal/dl.php?code=123abc


  2. This probably is the variant you are looking for. Both rules capture the actual argument embedded in the requested URL to re-use it in the internally rewritten request.

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [END]
    
    RewriteRule ^dl/([^/]+)/?$ dl.php?code=$1 [END]
    RewriteRule ^([^/]+)/?$ index.php?code=$1 [END]
    

    Yo may also want to add redirection rules to redirect clients using the "old" URLs:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [END]
    
    RewriteCOnd %{QUERY_STRING} ^code=([^/]+)$
    RewriteRule ^dl.php$ dl/%1 [R=301,END]
    RewriteRule ^index.php$ %1 [R=301,END]
    RewriteRule ^$ /%1 [R=301,END]
    
    RewriteRule ^dl/([^/]+)/?$ dl.php?code=$1 [END]
    RewriteRule ^([^/]+)/?$ index.php?code=$1 [END]
    

    For this it is a good idea to start out with a 302 redirection and only to change it to 301 when everything works as expected.

    Both variants use relative paths so that the rules work in distributed configuration files somewhere deeper than the document root of your http server. I personally prefer to implement such rules in the real host configuration instead of using distributed configuration files ("htaccess") for various reasons.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search