Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite not completely working

mod_rewrite not completely working

         

adambeazley

6:07 am on Jan 31, 2008 (gmt 0)

10+ Year Member



I don't know what i am doing wrong but some of my rewrites are working while others are not.

Basically i am trying to redirect some old wordpress post links to the new ExpressionEngine website posts.
Now I disabled the .htaccess file in the blog folder where wordpress was installed (it was rewriting blog/index.php to blog/) and i have one large .htaccess file in the root.

In that .htaccess the following lines of code that redirect actual .html files work just fine. The link gets redirected and the url gets updated.


RewriteRule ^example-text.html$ http://www.example.com/index.php/category/example-text/ [R=301,L]

Then later on in the same file I use the same technique to redirect the old wordpress blog links:


RewriteRule ^blog/2007/03/example-text/$ http://www.example.com/index.php/blog/example-text/ [R=301,L]
---AND I TRIED ----
RewriteRule ^http://www.example.com/blog/2007/03/example-text/$ http://www.example.com/index.php/blog/example-text/ [R=301,L]
---I ALSO TRIED ---
RewriteRule ^blog/index.php/2007/03/example-text/$ http://www.example.com/index.php/blog/example-text/ [R=301,L]

Those rewrites just dont work. They get the user to the website, but to some random page, not the post where it should be rewriting to.

Can someone please give me a hand, I dont know what is going on.

thanks

Adam

adambeazley

9:01 pm on Jan 31, 2008 (gmt 0)

10+ Year Member



Still hoping for some help. i tried commenting out alot of other stuff, but the problem persist. it seems like ExpressionEngine is reading the post plug as a series of tags and returning a page with a few results that match those tags.

Anyone have an idea?

jdMorgan

4:21 am on Feb 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem here is that we don't know the URLs that you are requesting, or the details of the URL-path that you need to rewrite to.

I can tell you that your first and third example rules above are acceptable, syntax-wise, while your second rule won't work except under very specific circumstances (not applicable to your situation).

The local URL-path requested by the browser goes in the pattern on the left, and the URL or URL-path to be substituted goes on the right. If you redirect to a new URL, and that URL matches another rule, then the redirected request will also get redirected or rewritten. So beware of interaction between rules -- This may in fact be why you are not getting the expected results.

To get a better view of the problem, I recommend using the "Live HTTP Headers" add-on for Firefox and Mozilla-based browsers to view the actual HTTP client request and server response headers -- If you are getting multiple redirects, this add-on will show them step-by-step. It's a free but indispensable tool.

Jim

adambeazley

5:47 pm on Feb 1, 2008 (gmt 0)

10+ Year Member



Ok I think i have an idea why this is happening, but I have no idea how to fix it.

Basically, i previously had wordpress installed in the /blog directory making my links look like website.com/blog/blah/blah/post.
But in reality there is a .htaccess file in the blog directory rewriting /blog/index.php to /blog/ here it is:


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

so in my new ExpressionEngine site, i have EE installed in the root making my real links website.com/index.php/template/post, but in my root .htacces i have a line of code that removes the index.php if a particular template is requested and "blog" is one of my templates.


AddType application/x-httpd-php .php .html .htm
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [nc]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond $1 ^(site存hop在log圭ommunity在egreen字esources存earch圯mbeds地bout圩eeds地dvertise奸egal同[0-9]{2,8}) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]

then i have the html rewrites that are working and then i have the blog rewrites as follows:


#rewriting all blog links to new EE links
RewriteRule ^http://www.example.com/blog/2007/03/neutral-existence-project/$ http://www.example.com/index.php/blog/welcome-to-neutral-existence/ [R=301,L]
# Blog categories
RewriteRule ^http://www.example.com/blog/category/bio-fuel/$ http://www.example.com/index.php/blog/biofuel/C7/ [R=301,L]
# Three keywords
RewriteCond %{QUERY_STRING} ^tag=([^\-]+)-([^\-]+)-([^\-]+)$
RewriteRule ^blog/index\.php$ http://www.example.com/index.php/blog/tags/tag/%1+%2+%3? [R=301,L]
# Two keywords
RewriteCond %{QUERY_STRING} ^tag=([^\-]+)-([^\-]+)$
RewriteRule ^blog/index\.php$ http://www.example.com/index.php/blog/tags/tag/%1+%2? [R=301,L]
# One keyword
RewriteCond %{QUERY_STRING} ^tag=([^\-]+)$
RewriteRule ^blog/index\.php$ http://www.example.com/index.php/blog/tags/tag/%1? [R=301,L]

So when i enable the .htaccess file in the blog directory, my old blog comes back online and when i disable it, the links don't work. Any idea how to fix this?

jdMorgan

6:17 pm on Feb 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You've got rules in conflict, trying to rewrite /blog<anthing> to /index.php/$1 in the first rule, and then redirecting ^blog/index\.php$ to various other URLs depending on the "tag" in the latter rules.

Also, if you rule order is as you say, then you've got problems there as well:

Put your external redirects first, in order from most-specific to least specific, and follow those with your internal rewrites, again in order from most-specific to least-specific. Generally, longest patterns first -- for example, your domain canonicalization redirect should be your last redirect, as it is a 'catch-all' for those URLs with incorrect domain that don't get taken care of by the more-specific preceding rules.

Generally the WordPress rewrites should be the last, since the only requirement to run them is that the requested URL does not resolve to an existing file or directory.

Sorry I can't help more; As noted above, I cannot tell what your real filepaths are and what your 'friendly' URLs are from what is posted.

Jim

adambeazley

3:32 am on Feb 8, 2008 (gmt 0)

10+ Year Member



I still cannot figure this out. what do you need from me in oder to give more specific help?

Ill try to give you more info here:
<snip>
Here is my .htaccess in the root folder of the site


AddType application/x-httpd-php .php .html .htm
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [nc]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
#rewriting all "green your" html links to new "Be Green" EE links
RewriteRule ^Home-Front.html$ http://www.example.com/index.php/begreen/green-your-home/ [R=301,L]
RewriteRule ^Home-Efficiency-Tax-Breaks-And-Rebates.html$ http://www.example.com/index.php/begreen/home-efficiency-tax-credits-rebates-incentives/ [R=301,L]
#
RewriteRule ^Automobile.html$ http://www.example.com/index.php/begreen/green-your-car-automobile-emissions-and-pollution/ [R=301,L]
RewriteRule ^Efficient-Driving-Habits.html$ http://www.example.com/index.php/begreen/fuel-efficient-driving-habits/ [R=301,L]
#
RewriteRule ^Intoduction-To-Alternative-Energies.html$ http://www.example.com/index.php/begreen/green-your-electricity-alternative-renewable-energy/ [R=301,L]
RewriteRule ^Solar-Power.html$ http://www.example.com/index.php/begreen/solar-power-for-the-home/ [R=301,L]
#
RewriteRule ^Carbon-Offsets.html$ http://www.example.com/index.php/begreen/green-your-travel-with-carbon-offsets/ [R=301,L]
RewriteRule ^Tree-Planting-Offsets.html$ http://www.example.com/index.php/begreen/tree-planting-carbon-offsets/ [R=301,L]
#
RewriteRule ^Green-Building.html$ http://www.example.com/index.php/begreen/green-building-introduction/ [R=301,L]
#
RewriteRule ^Tell-A-Friend.html$ http://www.example.com/index.php/advertise/tell-a-friend/ [R=301,L]
RewriteRule ^Write-A-Congressman.html$ http://www.example.com/index.php/resources/list-of-current-us-representatives/ [R=301,L]
#
#rewriting all blog links to new EE links
RewriteRule ^http://www.example.com/blog/2007/03/neutral-existence-project/$ http://www.example.com/index.php/blog/welcome-to-neutral-existence/ [R=301,L]
RewriteRule ^http://www.example.com/blog/2007/03/environmental-sustainablility-terms/$ http://www.example.com/index.php/blog/carbon-neutrality-sustainability-definitions/ [R=301,L]
#
# Blog categories
RewriteRule ^http://www.example.com/blog/category/bio-fuel/$ http://www.example.com/index.php/blog/biofuel/C7/ [R=301,L]
RewriteRule ^http://www.example.com/blog/category/general/$ http://www.example.com/index.php/blog/general/C9/ [R=301,L]
#
# Three keywords
RewriteCond %{QUERY_STRING} ^tag=([^\-]+)-([^\-]+)-([^\-]+)$
RewriteRule ^blog/index\.php$ http://www.example.com/index.php/blog/tags/tag/%1+%2+%3? [R=301,L]
# Two keywords
RewriteCond %{QUERY_STRING} ^tag=([^\-]+)-([^\-]+)$
RewriteRule ^blog/index\.php$ http://www.example.com/index.php/blog/tags/tag/%1+%2? [R=301,L]
# One keyword
RewriteCond %{QUERY_STRING} ^tag=([^\-]+)$
RewriteRule ^blog/index\.php$ http://www.example.com/index.php/blog/tags/tag/%1? [R=301,L]
#
#
RewriteRule (.*) /index.php?/$1 [L]
#
IndexIgnore .htaccess */.?* *~ *# */HEADER* */README* */_vti*
#
<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName example.com

Wordpress is installed in public_html/blog/ and the .htaccess file in that folder (written by wordpress) which is currently renamed and not working is:


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

I hope this helps and i appreciate your time. Please let me know if i need to provide you with more information. <snip> [T]he .html redirects work fine, but nothing else does.

[edited by: jdMorgan at 12:25 am (utc) on Feb. 9, 2008]
[edit reason] Edited for length, TOS 24 and 13 [/edit]

adambeazley

7:02 am on Feb 13, 2008 (gmt 0)

10+ Year Member



NOTHING?

Marcia

7:16 am on Feb 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What I've just found out is that it's posssible that it isn't the particular directives in question that are wrong, but that it's something else in .htaccess that's the problem.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com [nc]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

I had 301's and 410's not working, with just that code but when I added a redirect from index.html to the root example.com/ they started working just fine.

adambeazley

8:43 pm on Feb 14, 2008 (gmt 0)

10+ Year Member



i finally figured it out.
Basically I changed

RewriteRule ^http://www.example.com/blog/2007/03/environmental-sustainablility-terms/$ http://www.example.com/blog/carbon-neutrality-sustainability-definitions/ [R=301,L]

to

RewriteRule ^blog/2007/03/environmental-sustainablility-terms/$ http://www.example.com/blog/carbon-neutrality-sustainability-definitions/ [R=301,L]

Now i have one more little question. how to I make the trailing / not matter? In other words when someone goes to example.com/blog/2007/03/environmental-sustainablility-terms/ they get redirected perfectly, but when someone goes to te same link without that trailing slash:
example.com/blog/2007/03/environmental-sustainablility-terms
they do not get redirected correctly.

How can i fix this easily? without writing new rules for every one.

adambeazley

8:51 pm on Feb 14, 2008 (gmt 0)

10+ Year Member



never mind i just needed to put a . instead of a / and that did it.