Shorten URL with mod_rewrite in .htaccess file

Hi.
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
http://www.mydomain.com/some_verylong/path1234/ with
http://www.mydomain.com/short34/

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!

cla,

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

# .htaccess in DocumentRoot
# http://www.mydomain.com/some_verylong/path12[COLOR="Red"]34[/COLOR]/ => http://www.mydomain.com/short[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]

Regards,

DK

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
# http://www.mydomain.com/some_verylong/12[COLOR=red]34[/COLOR]path/ => http://www.mydomain.com/short[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!

cla,

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.

Regards,

DK

DK,
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…

cla,

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).

Regards,

DK

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?

ck,

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).

Regards,

DK

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,
http://localhost/abc/aaa/jj.php

to

[B]http://localhost/abc/aaa/[/B]

so can u help me?

ck,

.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).

Regards,

DK

DK,

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,
http://localhost/portal/vendor/product.php
so how can i short the url to this?
http://localhost/portal/vendor/
Mean the user can directly to my website folder?
thx.

ck,

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.

Regards,

DK

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,
http://localhost/portal/vendor/index.php
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?

ck,

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}.

Regards,

DK