...

View Full Version : Can I rewrite my urls to display as one directory higher?



cineweekly.com
01-10-2011, 02:11 AM
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

ShaneC
01-10-2011, 04:25 AM
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:

http://www.codingforums.com/showpost.php?p=1032840&postcount=4

cineweekly.com
01-10-2011, 04:54 AM
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:

http://www.codingforums.com/showpost.php?p=1032840&postcount=4

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.

ShaneC
01-10-2011, 05:07 AM
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:



RewriteEngine On
RewriteBase /

RewriteRule ^(.*)$ sunday/$1


Let me know what happens and we can take it from there.

cineweekly.com
01-10-2011, 05:35 AM
I am slightly familiar with .htaccess and have some redirects set up but mostly automated through hosting site.
I added:

RewriteBase /

...directly under 'RewriteEngine On' which was already there. Then I placed:

RewriteRule ^(.*)$ sunday/$1
...both first and last and I was getting "Internal Server Error" on my whole site. Sooo close.

ShaneC
01-10-2011, 05:59 AM
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).

cineweekly.com
01-10-2011, 06:03 AM
Here's what I have (previous stuff in red).


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]

ShaneC
01-10-2011, 06:37 AM
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:



RewriteRule ^/(.*)$ sunday/$1


Third, if you remove this RewriteRule does the server error go away?

cineweekly.com
01-10-2011, 06:47 AM
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.

ShaneC
01-10-2011, 07:03 AM
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!

cineweekly.com
01-10-2011, 08:10 AM
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

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

ShaneC
01-10-2011, 11:24 PM
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-sheets/mod_rewrite-cheat-sheet/

cineweekly.com
01-11-2011, 06:06 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum