Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts

    Can I rewrite my urls to display as one directory higher?

    If my articles actual location is:
    example.com/sunday/article.php

    How can I rewrite all articles in 'sunday' to appear as:
    example.com/article.php

  • #2
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Yes, you'll want to use the mod_rewrite apache module.

    Here's a link to a post where I provided a detailed explanation, as well as a very helpful tutorial, on how this can be achieved:

    Mod_rewrite Help
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #3
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by ShaneC View Post
    Yes, you'll want to use the mod_rewrite apache module.

    Here's a link to a post where I provided a detailed explanation, as well as a very helpful tutorial, on how this can be achieved:

    Mod_rewrite Help
    I keep re-reading but I can't customize Apache stuff at all. I have no idea on the syntax and codes or anything. Could you give me the code to make every url in a sub-folder appear like it's in the root folder and then I can just switch out sub-folder names to customize it? I don't have any dynamically created pages.

  • #4
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    The method there is geared towards dynamic pages but it can be adjusted to static as well. As for modifying Apache you can use this with an .htaccess file. Create a file named ".htaccess" and place it in the root of your web directory (typically public_html/ or www/).

    Now unfortunately I don't have access to an Apache installation and I'm terrible at REGEX. So I'll do my best to help you out however if anyone else is viewing this that has a better method please chime in.

    In your .htaccess file try putting in:

    Code:
    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^(.*)$ sunday/$1
    Let me know what happens and we can take it from there.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #5
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts
    I am slightly familiar with .htaccess and have some redirects set up but mostly automated through hosting site.
    I added:
    Code:
    RewriteBase /
    ...directly under 'RewriteEngine On' which was already there. Then I placed:
    Code:
    RewriteRule ^(.*)$ sunday/$1
    ...both first and last and I was getting "Internal Server Error" on my whole site. Sooo close.

  • #6
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Did you also include RewriteEngine On? And your .htaccess, or the rewrite part anyway, should appear exactly as in my post (in that order and all).
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #7
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Here's what I have (previous stuff in red).

    Code:
    ErrorDocument 404 /errors/404.php
    
    RewriteEngine on
    RewriteBase /
    
    RewriteRule ^(.*)$ sunday/$1
    
    RewriteCond %{HTTP_HOST} ^cineweekly.com$
    RewriteRule ^(.*)$ "http\:\/\/www\.cineweekly\.com\/$1" [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^u-stu.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.u-stu.com$
    RewriteRule ^/?$ "http\:\/\/www\.cineweekly\.com\/" [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^cineweekly.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.cineweekly.com$
    RewriteRule ^index\.php$ "http\:\/\/www\.cineweekly\.com\/" [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^cineweekly.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www.cineweekly.com$
    RewriteRule ^images\/?$ "http\:\/\/www\.cineweekly\.com\/" [R=301,L]

  • #8
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Edit: These won't work. I'm trying some things on my Apache server. I'll post back if I have anything.


    Okay, three ideas.

    First, try putting the RewriteRule I posted below all the other RewriteRules.

    Second, try using this RewriteRule instead:

    Code:
    RewriteRule ^/(.*)$ sunday/$1
    Third, if you remove this RewriteRule does the server error go away?
    Last edited by ShaneC; 01-10-2011 at 05:47 AM.
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #9
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts
    First option = same problem.

    RewriteRule ^/(.*)$ /sunday/$1 = no error but urls are the same even after clearing cache etc. (so basically it doesn't do anything)

    Third option = no error without your first code.

  • #10
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    Unfortunately none of my tests cleared your problem and I can't devote any more time to it at the moment.

    I did, however, find something that may help here: http://forum.modrewrite.com/viewtopic.php?f=3&t=6835

    Try out some of those methods and post back. As soon as I get some more time I'll try some different methods. I'm surprised this is proving so difficult!
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #11
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts
    I'll check the site out. Thanks for your help so far. My difficulty is I can't find anywhere to learn what ^ / $ (.) or the order of any of that does. I know * is a wild card but that's about it and it makes it impossible to play around with it.

    EDIT: I'm sooooooo close now. The following changes my url:
    root/sunday/file <--old url
    root/file <--new url
    Code:
    RewriteRule ^sunday/(.*)$ /$1 [R=301,NC,L]
    But it goes to a 404 page because the file really is in "root/sunday/file" I just want it to display as "root/file".

  • #12
    Codeasaurus Rex
    Join Date
    Jun 2008
    Location
    Redmond, WA
    Posts
    659
    Thanks
    31
    Thanked 100 Times in 94 Posts
    That actually isn't doing what you want it to do, I think.

    What you have set up right now is a redirect. So it's redirecting from root/sunday/file to root/file. It's not, which I think you want it to, seeing root/file as the URL but looking in root/sunday/file for the file.

    Am I correct in assuming that's what you want it to do?

    I'll keep trying some more things. Here's a great reference for knowing what the ^, $, (.), etc do: http://www.addedbytes.com/cheat-shee...e-cheat-sheet/
    Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
    My posts, comments, code, and suggestions reflect only my personal views.
    Web Portfolio and Code Snippets: http://shanechism.com

  • #13
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts
    You're exactly right. That's why I tried removing the R=301 to see if you would just "rewrite" the displayed url. Thanks for the cheat sheet it clears up a lot.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •