How I add new rewriterule in WordPress? - php

SOLUTION: I put this line in .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
-->RewriteRule ^event/([^/d]+)/([^_]*)/?$ index.php?pagename=event&eventid=$1 [QSA]<--
RewriteBase /teatro/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /teatro/index.php [L]
</IfModule>
The final url is : www.events.com/teatro/event/2/ExampleEVENT(this is not relevant)/
this is my first question and sorry for my English.
Before start my question I have seen some similar questions about this and I found "the solution" but in my case doesn´t work.
I found this link: https://wordpress.stackexchange.com/questions/5413/need-help-with-add-rewrite-rule
Ok. My question started because I have a plugin in Wordpress (EventBooking) and when I create a new event in the BackEnd the url for default in the FrontEnd is this:
www.events.com/teatro/event/?eventid=2
And I want a friendly url like this :
www.events.com/teatro/event/[name of event] (whitout id).
in the .htaccess I have this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /teatro/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /teatro/index.php [L]
</IfModule>
In the database the name of field of a Event is id not eventid but the original url that works in the frontEnd , is www.mydomain.com/teatro/event/?eventid=2
I think the problem is here. But I don´t know.
I tried add new Rewriterule in functions.php of my theme but doesn´t work.
Thanks

First of all change your permalink structure to /%postname%/ and then install this plugin "CUSTOM POST TYPE PERMALINKS", once you have done that, you'll be able to see a new taxonomy under your wordpress permalinks's page through which you'll be able to customize your custom post type permalink's structure, change it to /%postname%/%postname%/ to make it what you're looking to. If the existing existing taxonomy is %event-plugin% then do it accordingly.

Related

Difficulty creating custom RewriteRules for Wordpress

I am creating a website for a radio station and want to make a section of the site for artists and tracks that are played on the radio. I am currently migrating the code I have written to a Wordpress template, and I'm having difficulty replicating the RewriteRules I have created.
I want a user to be able to type:
/music/<artist name here>/
/music/<artist name here>/<track name here>/
And have it rewrite to:
/music/artist.php?artist=<artist name here>
/music/track.php?artist=<artist name here>&track=<track name here>
e.g.:
/music/Red+Hot+Chili+Peppers/
/music/Red+Hot+Chili+Peppers/Californication/
I was able to achieve this using the following .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^music/([^/]*)/?$ /music/artist.php?artist=$1
RewriteRule ^music/([^/]*)/([^/]{1,})/?$ /music/track.php?artist=$1&track=$2
When I switched to Wordpress, I began by trying replicate the artist page first, and have been unable to do so.
Being that the structure of Wordpress is different from a standard directory/file structure, I decided to try two methods:
Rewrite to /music/ for both of the rules, then decide to include either artist.php or track.php in the "music" page, based on the GET parameters.
Redirect to /artist/ or /track/ and have two different pages instead.
I have tried both methods and neither work.
For method 1, this is the code I added to functions.php (only trying the artist rewrite to begin with):
function custom_rewrite() {
add_rewrite_rule('music/([^/?]*)/?$', 'music/?artist=$1');
}
add_action('init', 'custom_rewrite');
Upon saving permalink settings (using "day and name"), this created the following .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^music/([^/?]*)/?$ /music/?artist=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Upon navigating to /music/<artist name here>/, depending on different permalink settings, I will either get a 500 error or I will be shown the home page.
For method 2, this is the code I added to functions.php:
function custom_rewrite() {
add_rewrite_rule('music/([^/]*)/?$', 'artist/?artist=$1');
}
add_action('init', 'custom_rewrite');
Upon saving permalink settings (using "day and name"), this created the following .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^music/([^/]*)/?$ /artist/?artist=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Again, upon navigating to /music/<artist name here>/, depending on different permalink settings, I will either get a 500 error or I will be shown the home page.
Is there something I'm missing in the .htaccess file? Am I somehow conflicting with Wordpress' permalink settings? Is this simply just not possible in Wordpress? Any help would be greatly appreciated.

.htaccess RewriteRule Not Working on Wordpress

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.

Can i add .htaccess for specific page in child theme using wordpress?

I'm trying to add .htaccess for specific page in wordpress child theme. Actually i'm sending category slug in url to custompostpage.php displaying post by category slug. For getting the category slug i'm using cp= action at end of the url.
http://example.com/demo/project/custom-post-page/?cp=search-engine-marketing
Functionality working fine.. But I want to remove the ?cp= for SEO purpose. For the task i'm using following .htaccess
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^custom-post-page/([^.]*)$ custom-post-page?cp=$1 [QSA,L]
</IfModule>
But it does not working. I upload this .htaccess file in the child theme (same path of custompostpage.php).
Can anyone guide me that can i add .htaccess file in the child theme using wordpress or not? If yes then where i'm doing wrong in my coding. I would like to appreciate if someone guide me. Thank You
The rewrite rule needs fixing as your url doesn't start with custom-post-page
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)custom-post-page/([^\.]*)$ $1custom-post-page?cp=$2 [QSA,L]
</IfModule>

Rewrite rule in wordpress for wordpress page

I'm running wordpress in my local with "ben.scbw.zec.dev" vhost.
I have created page template to display particular event detail.
http://ben.scbw.zec.dev/event-view?view_event=14
I can access page with above url "event-view" is slug on page and page id = 439 .
Now, I desire to access it with below url.
http://ben.scbw.zec.dev/event-view/this-is-test-event/14/
"this-is-test-event" its fro titel of event any text , i have manage to create URl.
Below is code from my .htaccess
# 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
Above is Standard wordpress crated code and i have added following code.
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^event-view/([^/]*)/([^/]*)/$ ./index.php?page_id=439&titel=$1&view_event=$2 [L]
here 439 is in page id for "even-view" as i mentioned.
But, I'm not able to access page with "http://ben.scbw.zec.dev/event-view/this-is-test-event/14/" url. it shows me 404 not found error.
I have been trying this for 4-5 day but not able to find solution.
I'm really very Thank full if anyone can help me in this.
First of all, wordpress uses internal RewriteRules to map an url.
So you don't have to add an additional RewriteRule-tag to your htaccess.
You have to register your query parameter and add rewrite rules to the internal wordpress rewrite engine.
#see https://developer.wordpress.org/reference/classes/wp/add_query_var/
#see https://developer.wordpress.org/reference/classes/wp_rewrite/

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.

Categories