Forum Moderators: phranque

Message Too Old, No Replies

~ remove from url

mod rewrite

         

djflakf1

7:47 pm on May 10, 2022 (gmt 0)

Top Contributors Of The Month



Hello i have little hosting for developing and im using accounts but to get access to them i need to use

example.com/~user

¿what can i do with mod rewrite to show the content of

example.com/~user in example.com/user?

phranque

11:13 pm on May 10, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], djflakf1!

if i understand correctly, you want to use mod_rewrite to internally rewrite requests for https://example.com/user to the internal /~user url.
the question that must be answered first is how do you identify which requests should be internally rewritten and which ones are legitimate urls such as these examples:
https://example.com/privacy
https://example.com/contact
https://example.com/about
https://example.com/logout
etc

are you currently using any other mod_rewrite directives?

djflakf1

12:42 am on May 11, 2022 (gmt 0)

Top Contributors Of The Month



I need everything written after the / to be read as if it had the ~

/user1
/day1
/teacher
/complete

I have many users in the /home folder but in order to see them I need to use the ~ and I don't want to force the user to use the ~

I also wonder, where would I put that ruler? httpd.conf? I need this to be true for all folders inside /home

and not, im not using otrer mod_rewrite directive

phranque

1:48 am on May 11, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I need everything written after the / to be read as if it had the ~

there are no urls on this site other than these /user urls?

I have many users in the /home folder but in order to see them I need to use the ~ and I don't want to force the user to use the ~

this part is easy.
defining the exceptions may be more difficult.

I also wonder, where would I put that ruler? httpd.conf? I need this to be true for all folders inside /home

httpd.conf is ideal if you have access, but knowing this is important to the solution.

and not, im not using otrer mod_rewrite directive

you probably should.
what are you doing for hostname canonicalization redirects?
i.e. http vs https requests or www vs non-www requests

djflakf1

3:59 am on May 11, 2022 (gmt 0)

Top Contributors Of The Month



without exception or for the first folder, for example

example.com/user1/(this part no)

if a amy want to see her file she can use example.com/amy no example.com/~amy

all i need is remove that tilde of the url but i don't know how to do it, i tried using this but is not working

[webmasterworld.com...]

https

phranque

4:37 am on May 11, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



the code i assume you tried as a starting point was this?
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /~mysite
RewriteRule (.*) http://example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]

(you should post an exemplified version of the code you tried)

this code will return an external 301 redirect of the request for the tilde url to the non-tilde url.
this is something that i would also suggest you need.
however it doesn't do the internal rewrite you need from the non-tilde url to the tilde url.

show us what you tried and we'll start from there.

lucy24

4:47 am on May 11, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /~mysite
RewriteRule (.*) http://example.com/$1 [R=301,L]
Perhaps I should not be reading this so close to bedtime, but wouldn't this simply go around in circles? Unless you include the same RewriteCond as in the second rule.

It also seems like the pattern should be
^~
(or ^/~ if the rule is indeed lying loose in config, as hinted earlier in the thread)
so that you needn't bother the server with Conditions unless the request at this point actually is for a with-tilde URL. And then you exclude the ~ from the capture.

Are these URLs to be indexed? If not, the external redirect may not be needed, since Duplicate Content isn't an issue.

djflakf1

11:38 am on May 11, 2022 (gmt 0)

Top Contributors Of The Month



the code i assume you tried as a starting point was this?
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /~mysite
RewriteRule (.*) http://example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]

(you should post an exemplified version of the code you tried)

this code will return an external 301 redirect of the request for the tilde url to the non-tilde url.
this is something that i would also suggest you need.
however it doesn't do the internal rewrite you need from the non-tilde url to the tilde url.

show us what you tried and we'll start from there.



Yes, I tried that formula because I saw that it had worked for a user in this forum but now I realize that it is a redirection, what I need is that if the visitor enters example.com/user in the browser, the content of example.com/~user. I'm sorry I sent that link. Please

lucy24

4:33 pm on May 11, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The question phranque has asked several times, and it’s absolutely essential to get an unambiguous answer, is:

Are there any URLs whatsoever that do not begin in ~ (tilde)? (Supporting files such as images and stylesheets don’t matter, because those can be exempted by other means.) You need to make sure you are not rewriting or redirecting URLs that aren't supposed to have a ~ tilde in the first place, such as /contact or /legal

But as I type this, I note that the examples are consistently in the form

example.com/user
and-that's-all. That means you cannot simply rewrite (or redirect, doesn't matter) to
example.com/~user
because that is still not a physical file.

Are these actually directories in the form
example.com/~user1/
example.com/~user2/
and so on?

This leads to the tangential question: If your users are likely to type in the URL without the leading ~ tilde, what is the tilde for? Casual research suggests it's going out of style anyway, with the possible exception of academic sites.

djflakf1

5:40 pm on May 11, 2022 (gmt 0)

Top Contributors Of The Month



The question phranque has asked several times, and it’s absolutely essential to get an unambiguous answer, is:

Are there any URLs whatsoever that do not begin in ~ (tilde)? (Supporting files such as images and stylesheets don’t matter, because those can be exempted by other means.) You need to make sure you are not rewriting or redirecting URLs that aren't supposed to have a ~ tilde in the first place, such as /contact or /legal

But as I type this, I note that the examples are consistently in the form

example.com/user
and-that's-all. That means you cannot simply rewrite (or redirect, doesn't matter) to
example.com/~user
because that is still not a physical file.

Are these actually directories in the form
example.com/~user1/
example.com/~user2/
and so on?

This leads to the tangential question: If your users are likely to type in the URL without the leading ~ tilde, what is the tilde for? Casual research suggests it's going out of style anyway, with the possible exception of academic sites.


1. "Are there any URLs whatsoever that do not begin in ~ (tilde)?" : No, there is not files or folders with ~ before the name.

2. I'll try to be more specific. Yes, you are right ( example.com/~user1 ) doesnt exist like a physical folder, the real folder is ( example.com/user1 ), but im using mod_userdir in the server and for that i need to put ~ (tilde) to go to the user folder ( example.com/~user1 ), i want to remove that ~. to allow to the user open the folder using ( example.com/user1 ). Looking for a lot of sites there is a not a completed solution of this and a friend told me "you can use mod_rewrite to allow it". i tried with mod_user disabled but using that way i can't open the user folder because says "404 no found"

phranque

6:55 pm on May 11, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you haven't yet posted an exemplified version of the code you tried.

djflakf1

7:31 pm on May 11, 2022 (gmt 0)

Top Contributors Of The Month



you haven't yet posted an exemplified version of the code you tried.


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /~mysite
RewriteRule (.*) http://example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]

phranque

10:01 pm on May 11, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



im using mod_userdir in the server and for that i need to put ~ (tilde) to go to the user folder ( example.com/~user1 )

actually:
This module allows user-specific directories to be accessed using the http://example.com/~user/ syntax.

source: Apache Module mod_userdir [httpd.apache.org]

RewriteEngine On

# this should 301 redirect any requests for /~user/ to https://example.com/user/
RewriteRule ^/~([a-zA-Z0-9]+)/$ https://example.com/$1/ [R=301,L]

# this will redirect any non-secure requests or any requests for subdomains (including www) to the https://example.com hostname
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^(example\.com)?$ [NC]
RewriteRule ^(.*)$ https://example.com$1 [R=301,L]


this should handle redirects of any requests for non-canonical hostnames and for users' directories containing the tilde.
the regular expression for matching a user name pattern may need some tweaking.
once that is working there will likely be additional tweaks needed to handle the contents of user name directories.
whether or not an internal rewrite is required to translate the requested path depends on what your UserDir directive looks like.
i'm not even sure it would work - it probably depends on the order of module execution, so this would take some testing.
at that point, i'm wondering why you're jumping through hoops to use mod_userdir while trying to avoid the tilde usage.
you can probably do it all with mod_dir.
are you using UserDir (dis/en)abled keyword directives?

djflakf1

11:04 pm on May 11, 2022 (gmt 0)

Top Contributors Of The Month



I'm wondering where to put this code.

httpd.conf? htaccess in /home directory?

djflakf1

11:10 pm on May 11, 2022 (gmt 0)

Top Contributors Of The Month



this should handle redirects of any requests for non-canonical hostnames and for users' directories containing the tilde.
the regular expression for matching a user name pattern may need some tweaking.
once that is working there will likely be additional tweaks needed to handle the contents of user name directories.
whether or not an internal rewrite is required to translate the requested path depends on what your UserDir directive looks like.
i'm not even sure it would work - it probably depends on the order of module execution, so this would take some testing.
at that point, i'm wondering why you're jumping through hoops to use mod_userdir while trying to avoid the tilde usage.
you can probably do it all with mod_dir.
are you using UserDir (dis/en)abled keyword directives?


I have CWP7 installed and when I create accounts to access those accounts I must use the ~ parameter, I tried to disable mod_userdire to see if that could open users without the ~ parameter but nothing. I have written in the CWP forums but they do not reply. What I have thought simply by searching many places is to keep the mod_userdir but looking for the way you suggest that when entering through /user1 it passes through /~user1 but actually opens the real content that is in /user1

phranque

1:21 am on May 12, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I'm wondering where to put this code.

httpd.conf?

the patterns specified in the RewriteRule Directives assume these directives are in httpd.conf, as discussed earlier.

when I create accounts to access those accounts I must use the ~ parameter

what does your UserDir directive(s) look like?

I tried to disable mod_userdire to see if that could open users without the ~ parameter but nothing.

"nothing"?
no response?
no clues in the log files?

What I have thought simply by searching many places is to keep the mod_userdir but looking for the way you suggest that when entering through /user1 it passes through /~user1 but actually opens the real content that is in /user1

you should be looking for content in https://example.com/~user1/ (a directory url) and not https://example.com/~user1 (no trailing slash)
what happens when you request this?
https://example.com/user1/

djflakf1

2:19 pm on May 12, 2022 (gmt 0)

Top Contributors Of The Month



the patterns specified in the RewriteRule Directives assume these directives are in httpd.conf, as discussed earlier.


[i.imgur.com...]

what does your UserDir directive(s) look like?


<IfModule mod_userdir.c>
UserDir disabled
UserDir enabled instru1
</IfModule>


"nothing"?
no response?
no clues in the log files?


No working without ~ to access to the golder

you should be looking for content in https://example.com/~user1/ (a directory url) and not https://example.com/~user1 (no trailing slash)
what happens when you request this?
https://example.com/user1/


404 no found

[edited by: phranque at 9:24 pm (utc) on May 12, 2022]
[edit reason] please exemplify domains [webmasterworld.com] [/edit]

phranque

9:22 pm on May 12, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



does the web server error log file give any clues?
this should tell you the fully qualified file path of the missing (404) file requested.

have you tried journalctl to check the system log files for clues?

<IfModule mod_userdir.c>
UserDir disabled
UserDir enabled instru1
</IfModule>

this seems like it would only enable one user:
https://example.com/~instru1/
all others would be disabled.

also note this from apache's mod_userdir doc:
User directory substitution is not active by default in versions 2.1.4 and later. In earlier versions, UserDir public_html was assumed if no UserDir directive was present.

i don't see anything that specifies user directory locations for translation.
where does this (/instru1/) directory actually exist?
what is the DocumentRoot directive's specified directory path?

lucy24

10:31 pm on May 12, 2022 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's explained a little more coherently in the tutorial [httpd.apache.org] (though not a lot more coherently).

Key point: there's a line missing from the UserDir directives. In addition to the disabled/enabled lines which specify what users are allowed, there also has to be a line setting the physical filepath(s) for all those example.com/~username/blahblah URLs.

It would admittedly be more intuitive if there were two different directives--one to spell out the users, one to declare the filepath--but it can't be helped.

djflakf1

10:41 pm on May 12, 2022 (gmt 0)

Top Contributors Of The Month



When you create an account using the CWP it generates its own vhost, in that file it is direct

<IfModule mod_userdir.c>
UserDir disabled
UserDir enabled instru1
</IfModule>


i don't see anything that specifies user directory locations for translation.
where does this (/instru1/) directory actually exist?
what is the DocumentRoot directive's specified directory path?


/home/instru1
/home/instru2
/home/instru3
/home/instru4
/home/instru5

but to open that i need use

hosting.example.com/~instru1
hosting.example.com/~instru2
hosting.example.com/~instru3
hosting.example.com/~instru4
hosting.example.com/~instru5

phranque

10:47 pm on May 12, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



it's also worth considering all the factors involved in Mapping URLs to Filesystem Locations [httpd.apache.org], several of which have been referred to in this thread and may come into play for the ultimate solution.

djflakf1

10:59 pm on May 12, 2022 (gmt 0)

Top Contributors Of The Month



It's explained a little more coherently in the tutorial [httpd.apache.org] (though not a lot more coherently).

Key point: there's a line missing from the UserDir directives. In addition to the disabled/enabled lines which specify what users are allowed, there also has to be a line setting the physical filepath(s) for all those example.com/~username/blahblah URLs.

It would admittedly be more intuitive if there were two different directives--one to spell out the users, one to declare the filepath--but it can't be helped.


i did this but i get 404

# Settings for user home directories
#
# Required module: mod_authz_core, mod_authz_host, mod_userdir

#
# UserDir: The name of the directory that is appended onto a user's home
# directory if a ~user request is received. Note that you must also set
# the default access control for these directories, as in the example below.
#
UserDir https://hosting.example.com/*/

#
# Control access to UserDir directories. The following is an example
# for a site where these directories are restricted to read-only.
#
<Directory "/home/*/public_html">
AllowOverride FileInfo AuthConfig Limit Indexes
Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
Require method GET POST OPTIONS
</Directory>

phranque

12:01 am on May 13, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i did this but i get 404

and what clues did you find in the web server error log?

UserDir https://hosting.example.com/*/

this should actually cause a 301 response.

djflakf1

12:47 am on May 13, 2022 (gmt 0)

Top Contributors Of The Month



finally i found the solution

adding this httpd.conf

AliasMatch "^/([a-zA-Z0-9]+)(/(.*))?$" "/home/$1/public_html/$3"

phranque

4:23 am on May 13, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



AliasMatch

you will want to do this using mod_rewrite instead of mod_alias directives.
When not to use mod_rewrite

mod_rewrite should be considered a last resort, when other alternatives are found wanting. Using it when there are simpler alternatives leads to configurations which are confusing, fragile, and hard to maintain. Understanding what other alternatives are available is a very important step towards mod_rewrite mastery.

Simple Redirection

mod_alias provides the Redirect and RedirectMatch directives, which provide a means to redirect one URL to another. This kind of simple redirection of one URL, or a class of URLs, to somewhere else, should be accomplished using these directives rather than RewriteRule.

when there are Redirect and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file

Source: [httpd.apache.org...]

(the above quoted section also applies to Alias and AliasMatch directives)

djflakf1

11:32 am on May 13, 2022 (gmt 0)

Top Contributors Of The Month



i tried to do with mod_rewrite for days with any results asking a lot of people.

phranque

9:49 pm on May 13, 2022 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



"/home/$1/public_html/$3"

this doesn't match what you tried here:
UserDir https://hosting.example.com/*/