homepage Welcome to WebmasterWorld Guest from 54.226.80.196
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
.htaccess with WP and a script conflict
Drachsi




msg:4544905
 1:05 pm on Feb 12, 2013 (gmt 0)

I have a site which is html and has a WP Blog. I need the following code for the Blog to work.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
but this stops a script from working in siteURL/general/name/index.php
Removing the above code, the script works but not the Blog.

I have no idea really what I am doing, but it seems like I need someting in .htaccess that lets both work?

Appreciate and solution.

Regards

Pete

 

lucy24




msg:4545134
 2:14 am on Feb 13, 2013 (gmt 0)

it seems like I need someting in .htaccess that lets both work

Yes, but you haven't given enough information. Except that I now know you are either an imperfect typist or you come from Scandinavia ;) As pasted-in, your rule says:

<IfModule mod_rewrite.c>
...
</IfModule>

"If mod_rewrite is installed..."
The envelope is unnecessary, because (a) either you have mod_rewrite or you don't and (b) if you didn't have mod_rewrite your site wouldn't work at all and (c) if mod_rewrite isn't installed-- or if your hosting setup doesn't allow you to use it-- you don't need this forum, you need a new host yesterday.

RewriteEngine On
"enable rewriting"
This is correct and needs to stay-- except that if you get rid of the envelope (not its contents, just the envelope) you may already have the line RewriteEngine On. You only need to say it once per htaccess, or once per envelope.

RewriteBase /
"if a rewrite target starts with anything other than a slash or the full protocol-plus-domain, put a / at the beginning meaning 'start from the domain root'"
Common rule but unnecessary because (a) / is the default and (b) the rule only kicks in if you don't start a target with {etc. as above} and you would never do this anyway.

RewriteRule ^index\.php$ - [L]
"If the requested file is called 'index.php' stop here and don't do any more Rewrites."
Standard in CMS boilerplate htaccess, but leaves a huge hole because it doesn't distinguish between human requests for "index.php" by name (bad) and internal requests for "index.php" plus stuff (good).

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

"If the request is for any file or directory that doesn't exist, serve content from 'index.php' instead."

Wha--? This is again standard CMS boilerplate, but it's wrong on several counts:
It doesn't distinguish between requests for pages and requests for supporting files (images etc.)
It doesn't distinguish between requests for files that don't exist because the CMS hasn't created them, and files that don't exist because you never said they existed (like a misspelled image name)
It's coded as a rewrite rather than a redirect, so you've crossed the line from Duplicate Content to Infinite URL Space.

Do you now know less than you did before?

Good :) That's a start.

Drachsi




msg:4545181
 6:37 am on Feb 13, 2013 (gmt 0)

Hi,

I tried your suggestions above and as soon as I removed something it failed. I have tried combining the 2 parts as below. Everything works except when a link is clicked, it cannot find the page. The WP Blog is at [drachsi.com...] and the page to show the files to click on (under Categories)is at [drachsi.com...] When any link is clicked the error message is "Page unavailable"

<code># BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

RewriteRule ^category(.*)/(.*)\.(php|js|css)$ [drachsi.com...]
RewriteRule ^category(.*)(/images)(.*)$ [drachsi.com...]

RewriteRule ^category/(.*/)*([^/\.]+)/([1-9][0-9]*)\.htm/?$ index.php?newid=$2&pg=$3[nocase]
RewriteRule ^category/(.*/)*([^/\.]+)/?$ index.php?newid=$2[nocase]
RewriteRule ^category/?.*$ [drachsi.com...]
</IfModule></code>

Scandinavia is close, I'm in the Bavarian Forest and don't have an idea what I'm doing, after all I'm 72 this year.

Thanks for your help, maybe you could still help me?

Regards
Pete

PS, hopefully there are no more spelling mistakes?

incrediBILL




msg:4545185
 8:00 am on Feb 13, 2013 (gmt 0)

RewriteRule ^index\.php$ - [L]


Your problem is quite obvious, it's the line above.

Once you start this rewrite rule it impacts all index.php pages in all subdirectories including "/general/name/".

Your second problem is this line:
RewriteRule . /index.php [L]


Which now redirects everything to the index.php page at the same directory level as the .htaccess file being executed.

What you need is to either make it specifically look for the index.php file in the WordPress directory or install an .htaccess file in "/general/name/" that disables that rule for that directory.

Another alternative would to move WordPress to a sudirectory and not the root and install the .htaccess file in that subdirectory. You could then redirect all requests to your canonical index pages for your domain to the WordPress subdirectory and it would outwardly appear to work the same as http://example.com would always pull up the blog as the primary page.

Those are your options, find one that works best for you.

Drachsi




msg:4545198
 8:38 am on Feb 13, 2013 (gmt 0)

What would be the code to put in the .htaccess file which will be located in the /general/name folder to disable the rule for that directory?

Appreciate your help
Regards
Pete

Drachsi




msg:4545200
 8:46 am on Feb 13, 2013 (gmt 0)

I have had a message that it is possible to exclude the folder "general" in WordPress?

Regards
Pete

lucy24




msg:4545308
 1:10 pm on Feb 13, 2013 (gmt 0)

RewriteRule ^index\.php$ - [L]
Your problem is quite obvious, it's the line above.

Once you start this rewrite rule it impacts all index.php pages in all subdirectories including "/general/name/".

Your second problem is this line:
RewriteRule . /index.php [L]
Which now redirects everything to the index.php page at the same directory level as the .htaccess file being executed.


Uhm ... Bill ... hate to disagree with the grownups, but I think you're transposing mod_rewrite and mod_alias. In mod_rewrite, a leading slash in the target means the same as a leading slash in an html link: it's relative to the root, not to the current directory. It's not the same as an opening anchor in the pattern, which is relative to the current directory.

Speaking of which: the first rule has an opening anchor-- and is presumably located in the top-level htaccess-- so it can only apply to the top-level index page.

What the CMS boilerplate is intended to do is pretty straightforward: Any requests for non-page files should be sent on their way; all other requests should be pointed to index.php -- except that we seem to be missing the part where the content of the original request gets shoved into a query string so index.php knows what the user wanted.

What the boilerplate does do may be another matter entirely :(

Drachsi




msg:4545314
 1:46 pm on Feb 13, 2013 (gmt 0)

Sorry everybody, I am totally lost. The reason I joined was to find an answer, the technical stuff is way over my head.

It seems what is needed is now understood, but how do I get a solution?

Regards, and thanks for taking time out
Pete

lucy24




msg:4545483
 11:26 pm on Feb 13, 2013 (gmt 0)

The reason I joined was to find an answer, the technical stuff is way over my head.

You may have come to the wrong forum. Over here we force you to learn at least a little bit of the "technical stuff". (Not all of it. Right now there are at least four unanswered posts in Apache because I don't even understand the question, let alone being prepared to make up an answer.)

I'm now trying to make sense of your posted code. Was that a direct cut-and-paste or is there stuff missing? Or did it come in via a third-party word processor that chewed up the original?

RewriteRule ^category(.*)/(.*)\.(php|js|css)$ {target here}
RewriteRule ^category(.*)(/images)(.*)$ {target here}

RewriteRule ^category/(.*/)*([^/\.]+)/([1-9][0-9]*)\.htm/?$ index.php?newid=$2&pg=$3[nocase]
RewriteRule ^category/(.*/)*([^/\.]+)/?$ index.php?newid=$2[nocase]
RewriteRule ^category/?.*$ {target here}


The form [nocase] is not technically wrong, but why expend the four extra bytes when [NC] does the same thing? Besides, it's wrong here-- not technically but functionally. You should never use [NC] in rewrites except hotlink and similar rewrites, which are a special case. Can you work out what number 2^8 is? That's how many different URLs you have created by attaching the [NC] flag to an eight-letter word.

And, conversely, where did the rest of the flags go? Every RewriteRule needs at a minimum the [L] flag unless there is a clear and specific reason for leaving it out. And redirects need an explicit R=301 or they will default to 302.

Answer after some right-clicking: The flags are there, they just got eaten by the auto-linking-- which in turn means the flags will lead to some kind of error, because they're not delimited by a space. (I tested. It's "only" a 404, because the bracketed stuff will simply be read as part of the target.)

And about that auto-linking: There's a general WebmasterWorld rule about mentioning your own site by name. In some forums there are extra reasons for not doing so, and this is one of those forums. You noticed that anything in http: // turns into a clickable link. Well, that's no use in Apache because we need to see the exact text you typed. Use example.com and the text will remain visible.

If you read any three random threads in this forum you will find g1 or someone like him reading the riot act about using .* or .+ in non-final position. Notably in the first rule:

^category(.*)/(.*)\.(php|js|css)$

What happens: mod_rewrite meets the word "category" and then proceeds to capture happily to the very end of the request, because Regular Expressions are greedy by nature. No, I do not know why RegEx terminology all has to do with food. "Flavors" instead of dialects, et cetera. It then sees that it was supposed to capture a slash, so it goes backward and backward until it comes to a slash, and then gulps up the whole remainder of the request. And then it sees that it has to leave room for a period after its second capture, so it backtracks again ...

And as for that
(.*/)*
in the third and fourth rules...

([^/\.]+)/
Huh. Did you get this piece from somewhere else? [^/]+/ is the standard pattern for capturing directories one at a time. And [^./] is even better because it stops before the final extension so you don't have to backtrack as much. Note that you don't have to escape the . inside of grouping brackets, since "anything that isn't a character" is obviously meaningless.

I kinda like that structure of the first rule with the $2.$3. But are those first two rules necessary at all? It sounds as if the rules were written to cope with pages containing relative links to subsidiary files. Since the pages themselves have been rewritten, not redirected, the browser "thinks" it is at
www.example.com/category/blahblah
and will therefore ask for supporting files at
www.example.com/category/etcetera
This means that every single request for an image or stylesheet has to be redirected. Surely it would be better to have the pages use absolute links with leading / so the request goes to the right physical location in the first place?

^category/(.*/)*([^/\.]+)/([1-9][0-9]*)\.htm/?

Oh, ouch. Oh, ouch. Please say you got this piece from somewhere else, and then we can jointly stomp on it without fear of offending anyone. Treated purely as a Regular Expression it's got more problems than I can count. It's the kind of thing I could use in my text editor on a single 800K document because if it takes too long I can always go clean the bathroom or something in the meantime. You don't want to put your server to that much work.

What we need you to do is explain in English* exactly what these last few rules are intended to do-- and what the potential requests will look like-- and then we can figure out how to make them more streamlined.

One thing in all rules is definitely right so make sure you don't change it: the leading anchor before "category". This tells the server that if the beginning of the URL isn't the word "category" --and the very very beginning isn't the letter "c"-- then stop right now and don't spend any more time on the rule.


* I don't mean "English, not German". I mean "in regular human language without trying to put it into Apache-speak or Regular Expressions" ;)

Drachsi




msg:4545572
 6:10 am on Feb 14, 2013 (gmt 0)

Thanks to all. Sorry, but I really think I am in the wrong Forum. I just needed help getting my .htaccess file to work, nothing else. The last post proved I am totally out of my league.

Regards
Pete

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved