How do I rewrite /*/ to /content/public/work/*

How do I rewrite /*/ to */content/public/work/
Learning htaccess mod_rewrite looks like it would take a long time as it seems tricky. (Most of my threads are about htaccess.)

I am not sure I understand what you want to redirect, and to where. But if we are talking base path, and making the files be loaded from another directory, give this a try.

[code]RewriteEngine on

RewriteRule ^content/public/work/(.)$ content/public/work/$1 [L]
RewriteRule ^(.
)$ content/public/work/$1 [L][/code]

The tricky part here is that you will need to make exclude rules for anything you will not want to redirect again, similar to what I did on the first RewriteRule.

TRD,

While I’m confused at what the OP is asking for, you need to be aware of the problems with the :fire: everything :fire: atom:

[rant #1]The use of “lazy regex,” specifically the :fire: EVERYTHING :fire: atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is “greedy.” Unless you provide an “exit” from your redirection, you will ALWAYS end up in a loop![/rant #1]

Regards,

DK

The above code caused an Error 500 Internal Server Error, so I had to remove it. I’ll slightly change my question.

I have a folder inside /public_html/content/work/ called clickrobot that I want to be accessible from http://example.com/work/clickrobot … (without the content folder)

so http://example.com/work/clickrobot shows the /public_html/content/work/clickrobot

Does anyone have any ideas?

@dklynn also any help?

That clarification helps a lot. :slight_smile: You might just need something as simple as this:

# If the request is for "work/clickrobot"
# then internally route it to "content/work/clickrobot"
RewriteRule ^work/clickrobot$ content/work/clickrobot

EDIT: Actually, because clickrobot is a folder, not a file, and presumably you want all the contents of clickrobot to be accessible in the same way, I probably should have done this:

RewriteRule ^work/clickrobot/(.*)$ content/work/clickrobot/$1

J_M’s second thought looks good to me.

Please note that his :fire: EVERYTHING :fire: atom only captures what is requested after ^work/clickrobot/ and the redirection is NOT to the same location (adding the content subdirectory before the ^work) to prevent looping.

Regards,

DK

Yes, that is why I mentioned that rules had to be added to exclude additional redirects. Though I see I should most probably have described it better.

Thanks for pointing it out

TRD,

No problem. Lots of noobies use (.*) without understanding the dangers so it HAD to be pointed out. I’d done that so regularly that I had created a list of “rants” and have an automated response with that one as the #1 rant.

Regards,

DK

Someone gave me a working line of code that can rewrite the folder properly. However it conflicts with the Stacey cms I am using.

# Rewrite /projects/* to /content/projects/*
RewriteRule ^(projects/.*)$ /content/$1 [L,NC]

However I am using the Stacey cms for my site whch comes with its own .htaccess file, and when I insert the new line of code into my existing .htaccess, it breaks my site. Now http://desbest.uk.to/projects/ shows a directory listing instead of a html file from the Stacey cms’s templating system rendering it.

I’m going to wager a guess that you put this new line at the end of the htaccess. Try instead putting it at the top, just below the “RewriteEngine on” line.

I put it just below the RewriteEngine on line and it’s still conflicting with the cms.

You’ll have to show your whole htaccess then. And also be more specific about how it conflicts. Describe the expected and actual behavior.

Here’s the whole htaccess file. It shows you what comes by default with the Stacey cms.
The modifications I was trying to make are at the bottom, mostly indented for differentiation.

I tried putting the “Rewrite /projects/*” line at the top, just below “RewriteEngine on”, and everything seemed to work fine, so I have to ask you again to be more specific about what you mean when you say it’s conflicting. What URL are you trying, what’s the expected behavior, and what do you observe?

It’s conflicting because as soon as I change the order of the RewriteRule ^(projects/.*)$ /content/$1 [L,NC] line to move it further up the htaccess file to get the following web address to work http://desbest.uk.to/projects/clickrobot it then stops the http://desbest.uk.to/projects address from working by showing a directory listing, when before it would show a HTML file.

des,

I’m not familiar with the Stacey CMS but I’ll bet that the directory redirection and the Stacey code conflict BECAUSE Stacey’s believes it in your DocumentRoot.

Try:

Keep the directory redirection in your DocumentRoot
Move the Stacey code to the Stacey subdirectory (work, I believe). Be sure to change the Stacey code to reflect its subdirectory and NOT the DocumentRoot

Regards,

DK

I tried that and it still doesn’t work. I uploaded the Stacey cms to the /stacey/ folder and changed the htaccess file there, and it still doesn’t work as it’s conflicting.,

des,

First, “Learning htaccess mod_rewrite looks like it would take a long time as it seems tricky. (Most of my threads are about htaccess.)” is a cop out. Have a read through the mod_rewrite tutorial at http://dk.co.nz/seo and ask questions if you don’t understand something (PM is faster than posting and waiting for me to find the post and reply).

Second, J_M has asked for “specificity” (my term for clearing stating your intended redirection) as well as your existing code (because you’re saying that the /*/ => */public/work/ does work (what in the world are you *'s for?). Since I’m not familiar with either the Stacey CMS or their mod_rewrite code, including that with YOUR mod_rewrite code insertions would be most helpful.

In the mean time, try adding the R=301 flag so you can SEE the redirection (if your code has been matched) as that will let you know whether the code is working … and which part of the code is working.

Finally, please let me echo J_M’s request for the expected and achieved URIs as that will give us a hint as to what’s going on with your unobserved code, too.

Regards,

DK

Stacey is a flat file CMS, so all the content and templates for the pages are on the file system to be edited with a text editor with FTP. All content for each page is stored in its corresponding content folder, and the .txt file is where all the variables for the content pages are stored. All pages are stored as folders, within the /content folder, as the Stacey CMS instructs.

http://staceyapp.com/images/folders-view.png

In Stacey, both /content/projects and /content/4.projects corresponds to http://example.com/projects.

Also /content/projects/one corresponds to http://example.com/projects/one, as you can have pages inside pages.

I have a folder inside the content/projects folder which eschews away from the normal using a .txt file with variables to generate the page with a chosen template from the templates folder, and have just put some html files in there instead.

When I access this html file http://desbest.uk.to/projects/clickrobot, I get an Error 404.

However when I delete all of the htaccess rules that Stacey gives me by default and replace it with RewriteRule ^(projects/.*)$ /content/$1 [L,NC] the above website link above loads perfectly.

I’ve isolated through trial and error, the lines which are causing the conflict.

# Rewrite /projects/* to /content/projects/*
RewriteRule ^(projects/.*)$ /content/$1 [L,NC]

# Rewrite any calls to /* or /app to the index.php file
RewriteCond %{REQUEST_URI} /app/$
RewriteRule ^app/ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?$1 [L]

# Rewrite any file calls to the public directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(.+)$ public/$1 [L]

When the first paragraph is enabled, and the second and third is commented out, http://desbest.uk.to/projects/clickrobot works, but http://desbest.uk.to/projects/ gives this image.

http://cdn.discourse.org/sitepoint/uploads/default/23049/d9d9884aadce1640.png

When the first paragraph is disabled, and the second and third is commented out, http://desbest.uk.to/projects/clickrobot fails to work giving an Error 404, but http://desbest.uk.to/projects/ loads perfectly well.

I am trying to fix this myself by searching for how to do rewrite conditions where something doesn’t match, so I can have Stacey do its thing except for certain folders. RewriteCond !^(projects/.*)$ /content/$1 [L,NC]
http://stackoverflow.com/questions/5352249/rewrite-rule-if-condition-is-not-matched




The * symbol means a wildcard, of which its value is determined by what the user inputs. In this case it means redirecting the URL /projects/clickrobot to the content/projects/clickrobot folder, so the URL would say the former in the address bar, but the server would be looking at the latter to find the html page to run.

Update
I’m getting closer to fixing this with independent learning. I added RewriteCond !%{REQUEST_URI} /content/projects/clickrobot/$ to one of the code blocks. Note the exclamation mark, but it’s still not working. Shouldn’t the exclamation mark prevent the condition from being matched, with the certain criteria?

# Rewrite any calls to /* or /app to the index.php file
RewriteCond %{REQUEST_URI} /app/$
RewriteCond !%{REQUEST_URI} /projects/clickrobot/$ /* desbest edit */
RewriteRule ^app/ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond !%{REQUEST_URI} /projects/clickrobot/$ /* desbest edit */
RewriteRule ^(.*)/$ index.php?$1 [L]

  # Rewrite /projects/* to /content/projects/*
  RewriteRule ^(projects/.*)$ /content/$1 [L,NC,R=302]

# Rewrite any file calls to the public directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !public/
RewriteCond !%{REQUEST_URI} /projects/clickrobot/$ /* desbest edit */
RewriteRule ^(.+)$ public/$1 [L]

des,

Thank you for the Stacey tutorial - it helped … a bit. On the other hand, it really helped to see your mod_rewrite code.

I believe that your use of NOT{Apache variable} as the text to match the regex in your multiple RewriteCond statements will cause your code to fail. Move the ! to the regex and see if that helps.

Additionally, the No Case flag is designed to work on the {HTTP_HOST} variable which is not case sensitive. Since the {REQUEST_URI} IS case sensitive (and Apache requires that the {REQUEST_URI} be accurate in the case of the letters, the NC flag should NEVER be used in a RewriteRule (and reserved for use against the {HTTP_HOST} variable in RewriteCond statements).

R=302 is a temporary redirect - is that what you intended?

My mind is not fully engaged this morning (if ever ;D ) but I would avoid using PHP’s comment tags on a line with mod_rewrite statements. It’s GREAT that you’ve commented your code but use a # to comment out everything on a line after the #.

Regards,

DK