Troubles Overwritting Wordpress Url Rewrites - php

so essentially right now I have a plugin enabled to allow me to use php in my pages. I've setup a page and set the permalink as the homepage. Doing this I'm able to make requests like
http://mysite.com/?profile=Rihanna
and the php code will execute based on the given artist.
The issue appears when I try to make this url more seo friendly, I add the following rewrite rule to my .htaccess
RewriteRule ^artist/(.*).html http://mysite.com/?profile=$1
thinking that it should work but it does not, it simply goes to the wordpress 404 page. Now if I redirect it to a regular html file instead of mysite.com/?profile=$1 it works just fine.
Does anyone have any idea what's wrong and how I can get it working properly?
Thanks

Make sure your rule is before all of your wordpress rules. Wordpress' rules route all requests through am index.php and since "artist" is probably a bad permalink, it'll return a 404.
Additionally, if you include the http://mysite.com in your rule's target, it will inherently redirect the browser as opposed to internally rewriting the URI. You should remove it and include an [L] flag so rewriting stops in the current iteration (thus the wordpress rules won't get applied):
RewriteRule ^artist/(.*).html /?profile=$1 [L]

Related

Rewrite Old dynamic URL to new Dynamic URL

My website was using the below URL format
localhost/loc-New-Delhi-India (loc - was common in all URLs and the text after it used to change)
Now, I have changed it to
localhost/New-Delhi-India/location (Removed the loc and placed it at the end of URL as 'location'). For this, I'm using the below rewrite rule
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/(location)?$ city-detail.php?cityurl=$1
cityurl grabs the string after 'localhost' and searches the db.
Now since the website is heavily indexed by Google and shared a lot on social media, I want to redirect those urls to the new URLs. I tried the below code, but this just doesn't work.
RewriteEngine on
RewriteRule "loc-(.*)" "^([A-Za-z0-9-]+)/(location)?$ city-detail.php?cityurl=$1" [R]
The new URLs are created successfully, but the old one's fail to redirect to the new one. I'm sure something's wrong but cannot find it. I researched the old questions but they are a bit different. Here, I'm not including any subdirectory after localhost and adding a directory 'localhost' after the city name.
Any help will be appreciated a lott :)
The following would probably work:
RewriteRule loc-(.*) $1/location [R=301,QSA]
RewriteRule ^([A-Za-z0-9-]+)/(location)?$ city-detail.php?cityurl=$1
The first rule will trigger a 301 redirect which will also tell any search engines that your resource has been permanently moved and that would probably make them change their indexes to reflect this. Once you've redirected the 2nd rule should trigger as normal.

how to find redirect source through htaccess in php?

I have a site with lots of links that redirect to a small set of links.
I used the htaccess redirect 301 to redirect a user going to www.example.com/something/something_else/ , to www.example.com/my/collective/url/index.php where something and something_else are not actual folders but just keywords.
Is there a way in index.php to know which url it was redirected from?
I tried $_SERVER['HTTP_REFERER'] but it gave me nothing.
EDIT:
the rewrite rules I used are as follows:
RewriteRule ^something/something_else/(.*)$ /my/collective/url/$1 [R=301,L,NE,P]
UPDATE:
I eventually added a php script for the rewrite, in order to get the HTTP_REFERER. Seems there is no way to properly get it, since after redirect the HTTP_REFERER turns to the redirected url or disappears entirely.
Would still be happy to get an answer though if there is a way.
You can use $_SERVER['REQUEST_URI'] this will not be affected by a RewriteRule, but will not do any good no a pure redirect.
Instead of using a line like so:
RewriteRule ^.*$ index.php [R=301,L]
You could remove the R=301 part to not change the page location viewed by the browser, that way, the user thinks he really landed on your page using the /something/something_else link. This is also perfect for SEO... Just look out to not create too many links pointing to the same content...

Rewrite to add a query string in wordpress

I know this can be done using a wordpress function but I'd like to add this rule in my htaccess file.
Basically I want to add a parameter to the URL and then let wordpress go about its business..
I have this
RewriteRule ^events/all/ /events/?all [L]
But I'm getting a 404 when I try and go to /events/all/
Thank you for any advice
Joe
Make sure you add that rule before any wordpress rules. Wordpress routes everything to index.php and if the routing happens before any specific rewrites that you have, those rewrites will never get applied.
Additionally, wordpress uses the request information to handle routing, so it may see /events/all/ as the $_SERVER['REQUEST_URI'] var and get confused. So wordpress may not be able to go about its business, depends on how you've setup the wordpress end of things.
If for whatever reason it's still not playing nice with wordpress, you could try adding the P option to internally proxy the request:
RewriteRule ^events/all/ /events/?all [L,P]

.htaccess & wordpress site - How to add 301 redirect rules for no longer valid permalink?

I recently did a big update on my wordpress site, thus some old permalink are invlid now.
Seach google for a tutorial on this and found a lot about static html page redirection examples or specific php page which are not what i'm looking for
Both my old and new permalinks are in path format, for example, i need redirect a couple urls of old gallery posts (deleted) which are
/2009/06/gallery/abc/
/2009/06/gallery/cba/
/2009/06/gallery/bbc/
/2009/06/gallery/aab/
to a new page which is
/gallery/
How do i write a correct redirect rule for this?
RewriteRule ^[0-9]{4}/[0-9]{2}/gallery/.*$ gallery [R=permanent,L]
Should get you to where you need to be. If you need the actual parameters (IE Date / Month / Title) passed along it is a bit of a change up, but do-able.
EDIT
Fixed typo.
Try something like this:
RewriteEngine On
RewriteRule ^/2009/06/gallery/\(?([^/]*)\/$ /gallery/ [R=301,L]
Personally, I would use the Redirection plugin rather than editing your .htaccess directly -- it's easier, less danger-prone, and will let you log what redirections have been happening. It will also track 404 errors so you can see if you've forgotten to redirect anything.
Then, if you're just redirecting a couple of fixed posts, it's just a matter of adding a simple rule for each redirect, with the old and the new URLs, on the Redirection configuration page. You can also use regular expressions, as with .htaccess, if you want to do anything more complicated.

Changing WordPress URL structure while maintaing the proper 301 redirects with mod_rewrite

I currently have a blog set up with WordPress using URLs like so:
www.domain.com/blog/?pid=384092817
This was before I knew anything about anything. There's a significant amount of content there now and I want to change my URL structure to:
www.domain.com/my-post-title-384092817
Is there a way to set up my .htaccess and mod rewrite so when users go to the first url they get a 301 redirect to the second URL? I know to change my WordPress address (URL) setting from www.domain.com/blog to www.domain.com and my permalink setting to /%postname%-%post_id%, but how do I tell the old URLs to redirect to the new ones?
Do you actually mean that when users go to the second URL, it will be rewritten to the first? That can be done with
RewriteRule /blog/.+-(\d+)$ /blog/?pid=$1
If you want to send 301 redirects from the old URLs to the new ones, then you can't really do that with an .htaccess file. The reason is that Apache's mod_rewrite doesn't have access to the title of your post, so it won't know what title to insert in the URL. You'd have to do that redirect with PHP.
EDIT: y'know what, actually that's not entirely true. You can do some pretty crazy things with the RewriteMap directive, such as instructing Apache to ask an arbitrary program to perform the rewriting, and in that way you could have, say, a PHP script that does some database work to figure out what the title is and rewrites the URL appropriately based on that. But that seems like a way overcomplicated solution and I wouldn't suggest doing it - it'll be much cleaner and easier to just send the redirect directly from PHP.
Depending on your WP version, you can just use the Permalink redirect plugin -- should do the trick for you within WordPress and without mod_rewrite. However, as of WordPress 2.3, a lot of this should work automatically. At that point, the only thing you should have to do is redirect all your /blog/... requests to the route, which you can do via mod_rewrite roughly like this:
RewriteRule ^blog(.*) $1 [QSA]

Categories