Shorten URL with mod_rewrite in .htaccess file

I am trying to apply a mod_rewrite rule to one of our websites but none works. I wonder is somebody can help, please.

I would like to replace with

here is what I tried:
in .htaccess file I already have turned on the rewrite engine to prevent image hotlinking:

RewriteEngine on

#directives against hotlinking…

after I tried in turn each of the following variations:[LIST=1]
[]RewriteRule ^/short34$ /some_verylong/path1234
]RewriteRule ^/short34(.) /some_verylong/path1234$1
]RewriteRule ^short34/([A-Za-z0-9-]+)/?$ some_verylong/path1234$1 [L]
[]RewriteRule ^(.)/short34/(.*)$ $1/some_verylong/path1234/$2 [L,R=301][/LIST]None works. I am obviously missing some important detail here, but I can’t figure out what. Is there a RewriteBase rule missing?
Please help.

Thanks in advance!


You have shown a trailing slash in your test URLs but have not accounted for the / character in your regex.

# .htaccess in DocumentRoot
#[COLOR="Red"]34[/COLOR]/ =>[COLOR="Red"]34[/COLOR]/
# where [COLOR="Red"]red[/COLOR] are variables to be matched and used in the redirection
RewriteRule ^/?some_verylong/path12([0-9]+)/$ short$1/ [R=301,L]

This code REQUIRES the trailing slashes be present in the URL. IMHO, they should be optional in the “from” and NOT present in the “to” redirection.

RewriteRule ^/?some_verylong/path12([0-9]+)/?$ short$1 [R=301,L]



Hi DK.
Thanks for you feedback.
I could not test yet the solution you proposed, because I need our sys admin to restart apache for me and he is not there at the moment.

I must apologize: I realized that I inadvertently exchanged the order of some characters in my example (1234path instead of path1234); I prepared a corrected version of the code you provided, which I could not test yet.
Would a simple relocation of “path” as below be correct?

# .htaccess in DocumentRoot
#[COLOR=red]34[/COLOR]path/ =>[COLOR=red]34[/COLOR]/
# where [COLOR=red]red[/COLOR] are variables to be matched and used in the redirection
RewriteRule ^/?some_verylong/12([0-9]+)path/$ short$1/ [R=301,L]

Thanks again!


That looks fine to me.

BTW, if you’re using Apache 1 (and aren’t worried about an upgrade to Apache 2), you can remove the ? in the leading ^/?. I have to do that to be usable with both Apache versions.



thanks but I cannot get this to work. I am now to the point of giving up and just use a 301 redirect to achieve the same…

I hoped there was a method that would allow me to hide the “real” path from the visitor once the page is loaded; I mean would it be possible for static pages to redirect the long path to a short one? I guess not, unless I am physically creating the directory with the short path and moving all wht is under the long path and doing the redirect the other way around…


RewriteRule ^/?some_verylong/12([0-9]+)path/$ short$1/ [R=301,L]

MUST display the new location - because of the R=301! Eliminate that (and the comma) and you’re home free (so long as you understand what happens with the relative links in that page).



can u pls explain me how can I easily understand the .htaccess?
I want short the url, for example:

http://localhost/abc/abc/abc.php to http://localhost/abc/abc/

so, how can i solve this problem?


Have you read the tutorial linked in my signature? It’s ALL in there WITH code examples.

As for your “problem,” there’s not enough information to create your specification which is necessary for anyone else to understand what you’re trying to do. If you can explain EXACTLY what your intent is, then I can help you with the code you’re attempting to use (in other words, please show us your attempt - it’s to help you learn and, in doing so, it will allow others to learn, too).



Thx for reply.

Actually, i new in php so i don’t understand how to use .htaccess to short the url, because i want avoid my website link be a hotlink, so i want to short the url then the user cant enter to certain page easily.

so how can i short the url to short by the simple way? for example,



so can u help me?


.htaccess is an Apache file and it has nothing to do with PHP. Since it’s normally images which are hotlinked and they’re normally linked to directly, the request (to shorten a url to hide a file) doesn’t make sense (if it can be displayed, it can be linked to … DIRECTLY).

REPEAT: What is your SPECIFICATION (there is no reference to jj.php in the UNDO THIS REDIRECTION in your example), it’s apparent that you’ve NOT read the tutorial and you’ve NOT shown what you have attempted (mod_rewrite code). Remember, I’m here to TEACH, not to code for free - especially when there’s no specification of what you REALLY want).




Ok, just now i already read your tutorial, but i not really understand so i wanna ask you.

This this the coding i try,but can function.

RewriteEngine On
RewriteRule ^/?portal /12([0-9]+)vendor/[R=301,L]

so which part is not correct, so can you tell me?
The original url is like this,
so how can i short the url to this?
Mean the user can directly to my website folder?


You’ve missed a very important point: YOU create the new format! If your new format is portal/vendor/, what part of that tells Apache that you are seeking the product.php script? NOTHING! When you create a “specification” for redirection, you must think of these things because Apache cannot read your mind - neither can I for that matter.

As for your code, the redirection (the part after the second space) cannot contain regular expressions (i.e., ([0-9]+)) and there MUST be a space before the flags. Please RE-read the tutorial as it has helped many members.

Finally, if all you want is to use product.php as the DirectoryIndex file, put it in the .htaccess file of the portal/vendor directory as

DirectoryIndex product.php index.php index.html

which will serve the first file in the list that is found when there is no file specified.



sorry DK, i very understand the tutorial that u posted. I very confuse about variable for .htaccess. so could u explain me by a simple URL?

For example,
the directoryIndex is http://localhost/ ? correct or not?

portal and vendor is a folder, all the php file is store in the vendor folder, so how can i use the variable to replace the folder name?


Gosh, this is an OLD thread! What happened?

Okay, DirectoryIndex is the default file which Apache is supposed to serve when the directory is specified but not the file. With Apache, the directive is DirectoryIndex index.php index.html index.htm default.htm, in other words, it’s the directive DirectoryIndex followed by a list of file names - the first found will be served.

Back to your initial post: If all you’re doing is a couple of redirections, you SHOULD be using mod_alias’s Redirect {new_format} /{absolute_path_to_new_file}.