change query string url using htaccess in wordpress - php

I want to change url structure of page in wordpress
from:http://example.com/page-name/?id=rd123
to:http://example.com/page-name/rd123/
I have tried below code in .htaccess, I am getting ID if i use id numeric(123), but I am getting 404 page not found error if use id alphanumeric(rd123).
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^page-name/([a-zA-Z0-9-]+)/?$ pagename.php?id=$1 [L]
</IfModule>
what i am missing in above code, it is in wordpress and i am not getting any perfect solution. here, page-name is template.

In this case, use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page-name/([a-zA-Z0-9-]+)/?$ page-name/index.php?id=$1 [L]
</IfModule>
It's not a problem with alphanumeric...

Try This,
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^page-name/(.*)/?$ page-name/index.php?id=$1 [L,QSA,NC]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
# END WordPress

Related

Why are all my wordpress pages redirecting to home?

I have a page call inventaires, the permalink is mywebsite.com/inventaires
Whenever I try to reach it I am redirected to the homepage mywebsite.com
My htc access is as follow :
# 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
Does somebody know what's wrong ?
My solution :
Clear cache of the browser. It was using a wrong cached version. :(
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>
# END WordPress
try this code

RewriteRule not working on production server

For my current project I have to Redirect 301 some links but when you enter them with some extra get parameters that parameters need to be suffixed on the new url.
Example:
Old:
/language/nl/article-1/?test=123
new:
/language/nl/fa1-artcile-1/?test=123
So I use the following code: (which works fine on my dev env)
RewriteEngine On
Options +FollowSymLinks
RewriteBase /language/nl
RewriteRule /artcile-1/* /language/nl/fa1-artcile-1/$1 [R=301,L]
But once on my production env it does not work, it still redirects to new url but, the get parameters are not appended on the new url.
Edit: It does redirect but it does not append the parameters.
Edit 2: full fill
# 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
The rewrite rule(s) come before the wordpress part and I have about 30 of them.
Any suggestions?
Have it like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^language/nl/article-1/?$ /language/nl/fa1-artcile-1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
You need to append the query string:
# 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,QSA]
</IfModule>
# END WordPress
Probably the other answers here are correct as well. But for my case they won't work.
The env it is running up is a MS Azure env, so I need to have a web.config file instead of a .htaccess file (how ever, it works for some part).
Thanks for all the quick help and thinking!

htaccess rewrite url rule

I'm new to htaccess and I need some help with a rewrite rule.
I wish to rewrite:
/all-accomodation/anything/ to: /portfolio-gallery/anything/
The 'anything' could be any slug.
One thing to note is that I have a page with a slug of 'all-accomodation' and when requested by itself shouldn't rewrite.
Here is my current htaccess file:
# 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
Thanks in advance,
Dave
You don't want to edit Wordpress's rewrite. Above that block, you can try:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^all-accomodation/(.+)(/?) /portfolio-gallery/$1$2 [L]
</IfModule>
This will 301 redirect all requests.

Access Standalone PHP File With Wordpress Installed

I have just installed wordpress on a site. However, I have some standalone php files that I use for analyzing database data on the site that now return a 404 when trying to access them.
For example:
www.mywordpresssite.com/myscripts/myscript.php
This returns a 404.
Here is 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>
Is there any way I can achieve normal access to the said directory/file but without upsetting the current functionality of Wordpress?
Thanks a lot
Change your .htaccess file to this
# 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>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/myscripts/(.*)$ [OR]
RewriteRule ^.*$ - [L]
</IfModule>

URL rewrite query_string in wordpress

I have a taxonomy search form in wordpress and the link output is
http://localhost/wp/?cityid=16
But i want it to be rewrited as
http://localhost/wp/cityid/16
.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wp/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]
</IfModule>
# END WordPress
I found this but could not implement to my case. Any help please?
Try this RewriteRule:
RewriteBase /wp/
RewirteRule ^wp/cityid/([0-9]+)$ ?cityid=$1 [L]

Categories