.htaccess RewriteRule Not Working on Wordpress - php

A client asked me to built them a site with WordPress. I'm not very good at WordPress so the solution may already be on here, but I've read about a dozen SO questions and haven't figured out an answer yet so I'd thought I'd post my own.
This is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^podcast/([^/]+)$ /podcast?slug=$1 [QSA,L] #I added this
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
With my file like this, it redirects me to my 404 page. Even though the URL is correct (/podcast/word) the browser console says it's a 404.
I then assumed if I added an "R=301" flag, it would work around that issue. But if I do this:
RewriteRule ^podcast/([^/]+)$ /podcast?slug=$1 [R=301,QSA,L]
Then my browser just takes me to /podcast?slug=word.
I'm not sure if this is caused by my .htaccess or if it's caused by the WordPress 404 file. On the off chance anybody wants to see it, my 404.php file is:
<?php get_header(); ?>
<section class="white">
<div class="wrapper internal">
<!--generic error message-->
</div>
</section>
<?php get_footer(); ?>
My .htaccess file worked fine when using Wolf CMS, but not with WordPress. Being as I'm still learning WordPress, any help is greatly appreciated.
EDIT
I'm using the slug to reference a podcast from an RSS feed. I'm looking to turn http://example.com/podcast/?slug=example-podcast into http://example.com/podcast/example-podcast, but whenever I do that I get a 404.

Try to replace your .htaccess with this 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]
</IfModule>
# END WordPress

Example: if I had my wordpress inside a folder named 'a' as a sub directory then I will edit my .htaccess like this
# BEGIN WordPress
<ifmodule mod_rewrite.c="">
RewriteEngine On
RewriteBase /a/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /a/index.php [L]
</ifmodule>
# END WordPress

Also you can set permalink to Post Name. This will fix if your url are define by a post name instead of php type of url.

Related

Rewrite rules issue with pretty permalinks in wordpress. Im almost certain my rewrite rules are screwing something up

This is going to be a long post, so please bear with me.
Here is my directory structure
public_html
agentc0re
blog
Rewrite rules are enabled and do work.
Rewrite rules for public_html hide agentc0re and redirect to index.php
public_html .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?agentc0re.com$ [NC]
RewriteCond %{REQUEST_URI} !^/agentc0re/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /agentc0re/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?agentc0re.com$ [NC]
RewriteRule ^(/)?$ agentc0re/index.php [L]
</IfModule>
My index.php is designed such that it loads the specific page & then my header so i have my menu on every page.
http://domain.com/index.php
<?php
if(isset($_GET['page'])) {$page = $_GET['page']; /* gets the variable $page */}
//else{$page = 'index.php';}
if (!empty($page)) {
$page .= '.php';
include($page);
} /* if $page has a value, include it */
else {
include('home.php');
} /* otherwise, include the default page */
include 'header.php';
?>
This results in loading pages as such, http://domain.com/index.php?page=(blog|forum|....etc)
My agentc0re folder has the following rewrite rules such that it replaces the above with just http://domain.com/(Blog|Forum...etc)
/agentc0re/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /agentc0re/
RewriteCond %{HTTP_HOST} ^(www\.)?agentc0re\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/(index\.php)?[\s?] [NC]
RewriteRule ^ /Home? [R=301,L]
RewriteRule ^Home/?$ /index.php?page= [L]
RewriteRule ^Blog/?$ /index.php?page=blog [L]
RewriteRule ^Forum/?$ /index.php?page=forum [L]
RewriteRule ^SubCounter/?$ /index.php?page=SubCounter/subCounter [L]
RewriteRule ^AboutMe/?$ /index.php?page=aboutme [L]
RewriteRule ^Contact/?$ /index.php?page=contact [L]
</IfModule>
You see i just use an upper case version of the game to differentiate the actual pages, IE blog.php AKA blog(Oh yeah forgot to mention that my above php code removes the .php so i don't need to add it in the rewrite rule)
So now, http://domain.com/Blog works. The formatting sucks but whatever.
Here is the problem
The pretty permalinks do not work when they are enabled.
/agentc0re/blog/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
My Desired Result
When you browse to or click on the link that would take you here:
http://domain.com/Blog/admin/whats-up-world/
Works and goes to the first posting titled "whats up world" My pretty permalinks in wordpress is set to be:
http://domain.com/Blog/%author%/%pagename%/
Alrighty I think the book is done. If you need anymore info let me know.
Hosting through Godaddy if it's to any help.
Thank you!!
-Jon
Edit 1
I forgot that i should add two settings that might be relevant to the issue.
Wordpress URL: http://domain.com/agentc0re/blog
Site URL: http://domain.com/Blog
Edit 2
Just realized that when you would click on a menu link that it would then "double" that as a new link. IE: you first load http://domain.com/ and you get the home page. Then you click on Blog. It takes you to http://domain.com/Blog, but NOW everything in the menu bar says it will link you do http://domain.com/Blog/(Home|Blog..etc). I don't get it so i took all the rewrite rules for /agentc0re/.htaccess and commented them out for the time being.
To get pretty perma links to work with my setup all i needed to do was the following super simple change.
My wordpress .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ../index.php?page=blog [L,NC]
</IfModule>
Just needed to change the last line to be MY index.php which has all the includes for my header/menu and stuff.
There are a few issues unresolved, but im considering this answered because my main issue at the time was figuring out how to get the Pretty Permalinks working with my site.

Mod_Rewrite issue - Show Wordpress Content vs. Listing Directory

I have a Wordpress blog for our church. We have an actual directory called "sermons" and we also have a section on the site called sermons. I need to be able to go to http://ourchurch.org/sermons and get a listing of sermons or http://ourchurch.org/sermons/sermon-title/ and get the sermon.
However, if I go to http://ourchurch.org/sermons/speakername/sermon.mp3 I need it to play or download the mp3. The same would go for pdf files.
Everything is working EXCEPT the sermon listing page. When I go to: http://ourchurch.org/sermons it gives me a directory listing. I need it to allow the WordPress rules to work to direct it to the correct content page.
Here is what I currently have:
# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php
# 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]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/sermons/(.+)/*\.(mp3|pdf)$ [NC]
RewriteRule ^(.*)$ - [L]
</IfModule>
# END WordPress
What am I doing wrong?
Thanks in advance for your help!
Perhaps adding this at the top of your RewriteRule list?
It routes requests that end with sermons or sermons with a / to index.php - which is WordPress.
RewriteRule sermons/?$ index.php [L]

Wordpress install in root and in subfolder + two domains

God, this little problem drives me crazy and I really hope that you'll help me how to figure it out.
I searched over all previous threads and in the codex of wordpress, didn't find my exact problem.
This is it :
I've one host with a wordpress installed at the root. I've one domain pointing it. Everything is fine.
I've need now a second install of wordpress, that i've installed in a subfolder. I've a second domain, linked to this subdfoler.
Like this :
root > domain.net
root/subfolder > anotherdomain.net
If I let the "site url" setting and the "wordpress url" setting with "domain.net/subfolder", this is OK, I can access to my second site and all permalinks work.
But if I edit my site url/wordpress url in "anotherdomain.net" it renders the "domain.net" homepage without style...
I'm sure this is a question of HTACCESS but I can't find how to properly write it...
Thanks for your answers !
(sorry for my english, not my mother tongue)
EDIT : Here are .htaccess (at this state, I let them in order to access subfolder via domain.net/subfolder)
root:
# 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>
root/subfolder
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subfolder
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subfolder/index.php [L]
</IfModule>
# END WordPress
HTACCESS would be my first guess too, but another thought occurred to me: have you made sure that you are installing the two sites in two distinct SQL databases?
Assuming this is not the problem, is there any way you paste your htaccess files, so I can see if there is an issue in them?
All the config were good, the problem was with my host provider.
For the record, the htaccess for the site in the subfolder, when everything is ok :
# 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
And the site and wordpress urls pointing to the new domain.

URL redirection in Wordpress manually by editing .htaccess file

please help me to fix my one of wordpress problem with URL redirecting in PHP.
This is my original wordpress htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I want to create custom URL redirection here. For ex.,
I want to redirect pages,
http://example.com/b/1 > http://example.com/b?n=1
http://example.com/b/2 > http://example.com/b?n=2
i am using directory URL type (displaying URLs as directories without extensions) SEO options in wordpress settings.
inside of http://example.com/b page, I have included another php file using ‘include(‘z.php’);’ command.
Z.php is reading URL parameter comes from ‘/?n=1’ through redirected URL.
I tried below solution, but no luck..
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^b/([^/]*)$ /b/?n=$1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Appreciate any of help.
EDIT
wordpress header file can read the translated parameters. i checked that by displaying GET parameter 'n' in title.
but it shows file not found for http://example.com/b/
i verified that, by editing http://example.com/b/ page content. non of page content displaying after rewriting.
Any help appreciated
EDIT
URL may have characters in parameter as below.
http://example.com/b/abc_1 > http://example.com/b?n=abc_1
http://example.com/b/aa_2 > http://example.com/b?n=aa_2
Thanks for Helping
At first sight it seems to me that it should be
RewriteRule ^b/([^/]*)$ /?p=$1
Wordpress doesn't know what to do with the url /b?n=X.
If you want http://example.com/b/abc_1 to work, the abc_1 part must match the slug of a wordpress post.
Additionally you have to change the settings->permalink structure to match either postname or a custom structure like /b/%postname%.
http://example.com/b/1
to
http://example.com/b?n=1
Using this
RewriteRule ^b/([0-9]+)/?$ b?n=$1 [NC,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^b/([0-9]+)/?$ b?n=$1 [NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Test hear
More info
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^b/([^/]*)$ /b/?n=$1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
#add below line
RewriteRule ^b/([0-9]+)/$ http://example.com/b?n=$1 [L]
</IfModule>
I found the word press related solution for this question.
in word press all the requests are heading to index.php automatically. if we create custom rewrite to another page, that request not going through index.php. that makes wordpress unidentified request and wordpress will return page not found message.
becasue of that, we have to redirect custom rewrite to index.php instead of permalink.
i used
RewriteRule ^b/([^/]*)$ index.php?page_id=4&n=$1
this rule redirected all requests matched with rule to index page. (my page ID for /b/ is 4)
this worked successfully for me.
other solutions above worked for non wordpress situations. but i found wordpress behavior as i explained.
appreciates who tried to answer.

Website redirection problems

I have been working on a website that is primarily PHP based. When a user visits the site stuff like http://robroscoe.ca/index.php, is redirected to http://robroscoe.ca and I am okay with this. What I am not okay with is, my webpages like robroscoe.ca/cv.php when you type in robroscoe.ca/cv redirecting back to robroscoe.ca. I am not sure how this happened and I am wondering if someone could explain to me what is going on.
# 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
I think the .htaccess file has something to do with this but I am not sure how to modify this as Wordpress seems to have created this for me without me knowing it. Any help or reading materials would be appreciated.
Thanks for your time!
You problem is that you've created a PHP script that is separate from WordPress and are trying to access it through a 'pretty' URL which WordPress handles. WordPress does not know of a page called 'cv', and handles it like any other 404.
I would strongly recommend that you create the page within WP so it can manage its own 'pretty URL' structure.
If you're hellbent on shoehorning your own scripts in, then get cozy with hand-editing your .htaccess file for each one.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^cv/?$ cv.php [L] #new rule
RewriteRule ^index\.php$ - [L] #this is made redundant by the very next line.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Categories