Is there a way to make a URL appear as if it's one directory higher?

Discussion in 'Apache' started by CineWeekly.com, Jan 11, 2011.

  1. #1
    No one can seem to answer this simple question in any other forum. It's best to describe it in an example.

    Actual article location:
    example.com/folder/article.php

    Article appears in URL as:
    example.com/article.php

    I'm not actually moving the file so it's not a simple 301 redirect, I just want it to look like it's at a simpler URL. Thanks in advance.
     
    CineWeekly.com, Jan 11, 2011 IP
  2. palme

    palme Active Member

    Messages:
    320
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #2
    you can not reach directory highr. you can do it with your local copy of your site and the end directory is c drev or something like that , but in the ftp all functions depend of read/write right of your password and your user name and higher directory of your site in ftp is server root and you have no right to se or update that , but i think web admintrator of server he/she can do all with his/her password and username.
     
    palme, Jan 13, 2011 IP
  3. CineWeekly.com

    CineWeekly.com Greenhorn

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    You're making this way more complicated than it is. Look at the example in the OP, it's very simple. I'm not trying to edit the server root. I want items in my sub-directories to look like they're IN the root folder where I place the folder for that sub-directory and the same place I put my index.php and other files.
     
    CineWeekly.com, Jan 13, 2011 IP
  4. Riquez

    Riquez Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    enable mod_rewrite in Apache & use a .htaccess file to re-write the url

    RewriteRule ^/article.php$ /folder/article.php
     
    Riquez, Jan 14, 2011 IP
  5. Natcoweb

    Natcoweb Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hello,
    you could use .htaccess

    RewriteEngine on
    RewriteCond %{REQUEST_URI} !/folder/
    RewriteRule ^(.*)$ /folder/$1
    This will redirect everything to /folder/.
     
    Natcoweb, Jan 14, 2011 IP
  6. CineWeekly.com

    CineWeekly.com Greenhorn

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #6
    None of these work. Riquez, your option is doing the exact opposite of what I need. Natcoweb, I don't know what's going on with this option but it displays my pages without the CSS and doesn't do what I want.
     
    CineWeekly.com, Jan 14, 2011 IP
  7. Riquez

    Riquez Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    No it doesnt, it does exactly what you want, I have tested it.
    However, you might need to remove the initial / in the paths.

    RewriteEngine on
    RewriteRule ^article.php$ folder/article.php

    You have a real directory "folder" & inside a page called "article.php"
    With this rule when you link to or type the URL example.com/article.php it will keep that URL but it will actually be re-writing it to the real location of the file /folder/article.php

    Note that when you re-write a URL like this, you don't link your HTML to folder/article.php, you link to the re-write : article.php Apache does the rest.
     
    Riquez, Jan 14, 2011 IP
  8. CineWeekly.com

    CineWeekly.com Greenhorn

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #8
    So if I have a folder in my root called "sunday" and I want every article in that folder to appear as if it's in the root how would I do that? I don't want to add a new line for every article in the future. I'm trying (.*) and all that but can't get it working properly but I don't know much about Apache anyway.
     
    CineWeekly.com, Jan 14, 2011 IP
  9. Riquez

    Riquez Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    In that case you would be better using something similar to what Natcoweb posted.
    But you have a problem - if you have other files outside /folder/ its also going to apply to those.
    So if you link to a file in the root directory its going to still assume its actually inside /folder/
    You need a way to differentiate between them.

    As Natcoweb said:
    RewriteRule ^(.*)$ /folder/$1
    could be
    RewriteRule ^movie_(.*)$ /folder/$1

    Now as long as you make your link with "movie_" at the start it will use the files in /folder/
    eg: movie_something.php

    Really, you shouldnt need to be creating a new php page for each article. the-town.php, green-hornet.php, dilemma.php, etc.
    The sensible way would to have a database of articles & one single page article.php that displays the data for the article.
    Then you could use php & apache to re-write the url in a nice way for each article, but only have one php page & one rule.

    RewriteRule ^movie_([a-zA-Z0-9_-]+).php$ folder/article.php?name=$1

    With the above rule you can have URLs like

    example.com/movie_the-green-hornet.php

    but the actual script would be
    folder/article.php?name=the-green-hornet

    article.php would get your database content based on you tagging that article as "the-green-hornet" in the database.

    There are more complex ways to do it that are better, but thats just a simple example.

    If you are stuck with using /folder/ & multiple articles inside then the simple solution would be just to go with the longer url, but name your folder so it looks sensible.

    example.com/movies/the-green-hornet.php
    example.com/coming-soon/the-mechanic.php

    This is actually better with the folder in the url than without, because it gives more information to the user.


    This page is helpful when using mod_rewrite
    corz.org/serv/tricks/htaccess2.php
     
    Last edited: Jan 14, 2011
    Riquez, Jan 14, 2011 IP
  10. CineWeekly.com

    CineWeekly.com Greenhorn

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #10
    Thanks but I thought there would be a simple line of code that could take care of this simple problem. All I hear from Apache nerds is how amazing it is but it can't even make a url appear to be one directory higher.
     
    CineWeekly.com, Jan 14, 2011 IP
  11. Riquez

    Riquez Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    ha ha, ok, whatever.
    Well, you asked the original question & I gave you a 1 line answer that was exactly correct.
    but it turns out theres a whole bunch of other stuff you didnt tell us about.

    Why dont you just post the original rules from your website, otherwise we are just guessing how your site works.

    or better, learn about re-write rules & make your own.
     
    Riquez, Jan 14, 2011 IP
  12. CineWeekly.com

    CineWeekly.com Greenhorn

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #12
    Wow, I wasn't getting smart or anything so calm down. I said in the original post:

    Actual article location:
    example.com/folder/article.php

    Goal - article appears in URL as:
    example.com/article.php

    It doesn't get more simple than that.
     
    CineWeekly.com, Jan 14, 2011 IP
  13. Riquez

    Riquez Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    RewriteRule ^article.php$ folder/article.php

    That is the answer to the above.

    However, what you are not explaining - is "folder" always the same? or does it vary?
    is article.php always the same?
    If they change, how are they changing? what is the pattern?

    Without any actual world real examples we can only answer based on what you give.
     
    Riquez, Jan 14, 2011 IP
  14. CineWeekly.com

    CineWeekly.com Greenhorn

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #14
    I thought it was obvious that "folder" and "article" where examples. That's why I further explained that the folder is called "sunday" and the article names differ, there is no pattern. But that wouldn't matter because I want EVERY file in "sunday" to appear as if it's in the root.

    This:
    example.com/sunday/some-dumb-article-from-last-year.php

    Looks like:
    example.com/some-dumb-article-from-last-year.php

    Also, this:
    example.com/sunday/1002934629081487.php

    Looks like:
    example.com/1002934629081487.php
     
    CineWeekly.com, Jan 14, 2011 IP
  15. Riquez

    Riquez Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #15
    ok.
    We also need to know the actual file name of the script that displays your article & the parameters it takes.

    example.com/sunday/some-dumb-article-from-last-year.php

    Is this a genuine file?
    So if you FTP to your website you can see the folder "sunday" & "some-dumb-article-from-last-year.php" inside?
    Because it looks like this is already having some re-write rule on it.
    in which case the actual script reference would be
    article.php?day=sunday&post=some-dumb-article-from-last-year
     
    Riquez, Jan 14, 2011 IP
  16. Riquez

    Riquez Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    If its just for the days of the week that you want, then this should work.

    RewriteRule ^([a-zA-Z0-9_-]+).php$ monday/$1.php
    RewriteRule ^([a-zA-Z0-9_-]+).php$ tuesday/$1.php

    & repeat the line for each day of the week or other folder names.
     
    Riquez, Jan 15, 2011 IP
  17. CineWeekly.com

    CineWeekly.com Greenhorn

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #17
    Yes, there is an actual folder and file named those things. I haven't done any Apache stuff except the automatically generated redirect (non www to www) from cPanel. I tried RewriteRule ^([a-zA-Z0-9_-]+).php$ monday/$1.php and it 404s my index but the other pages aren't affected at all (still doesn't change the url). Your last post is what I've been expecting to do, one line of code for each folder, but I don't know much at all about Apache so it's hard for me to tweak anything.
     
    CineWeekly.com, Jan 15, 2011 IP
  18. Riquez

    Riquez Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    right, yep, my mistake there.
    I realise the problem now.

    Firstly, I think you are maybe mis-understanding something about how this works.
    So please just check this statement below & make sure you understand how re-write works.

    =====
    Re-write doesnt change a url, it allows a new url to map to somewhere else.
    So its no good typing "example.com/monday/another-article.php" in the browser & expecting it to change.
    After creating the rule, you now use "example.com/another-article.php"
    This applies to links on your website too.
    =====

    You want :
    example.com/sunday/some-dumb-article-from-last-year.php > example.com/some-dumb-article-from-last-year.php
    example.com/monday/another-article.php > example.com/another-article.php

    but do you see the problem? There is no information in the NEW url to tell which folder to use.
    You have to have something to indicate which folder you want to use.
    Apache cant just guess if "another-article.php" is in monday or tuesday.

    The previous rule will work
    RewriteRule ^([a-zA-Z0-9_-]+).php$ monday/$1.php
    If you use just this rule & then type the url in the browser example.com/article.php (where article.php is a file name inside your monday folder) it will work. But, it will also apply to index.php, as you noted. So its not really what you want.

    So, the only way to get this to work, is for you to decide on some way to indicate which folder the file belongs to.
    example.com/monday_another-article.php
    example.com/m_another-article.php
     
    Last edited: Jan 15, 2011
    Riquez, Jan 15, 2011 IP
  19. CineWeekly.com

    CineWeekly.com Greenhorn

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #19
    I think I see your point. The end result would still have been that the URL in the users address bar included the actual location. Other forums understood what I wanted but none of their attempts worked either. Thanks for your help. One more thing, would there be any way to make an htaccess file in the folder "monday" so that it wouldn't affect my root "index.php"? I don't know the semantics of Apache codes so let me explain it with my own code. If * equals any file and ../ works like in Dreamweaver (going back one directory) then:

    ChangeThis /*
    ChangeTo ../*

    If something like that were in the monday folders' htaccess file could it work? I just don't know how sites with a ton of articles can have site.com/article130147.php without clogging their root folder in cPanel or their ftp client.
     
    CineWeekly.com, Jan 15, 2011 IP
  20. Riquez

    Riquez Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #20
    I dont think you can do that.
    What you are asking for, in effect, is when someone clicks a link (or types it) like this
    example.com/monday/another-article.php
    Then it physically changes the url in the address bar to example.com/another-article.php.

    Its not a good idea because if someone bookmarks "example.com/another-article.php" it doesnt exist because its actually in monday.
    & as we discussed, we cant do the opposite ("example.com/another-article.php" maps to monday/another-article.php) because you are missing any info about what folder to look inside.

    Other sites have "site.com/article130147.php" because the content is in a database.
    For example, they have a page "article.php" - just that 1 page.
    All the articles are in the database.
    the link "article.php?id=130147" gets the article with that id number from the database & inserts it on the page.
    Then they use mod_rewrite to allow the url "article130147.php" > "article.php?id=130147"
     
    Riquez, Jan 15, 2011 IP