I'm doing SEO optimization for a website and I need to transform a part of the url into a parameter. The URL format can still be changed if needed.
Basically I'm working with Wordpress and want to create a single page that would work for all products, but I need to get a specific product number from the URL.
The URL looks something like this:
www.example.com/product/[product-id]-brand-name-and-some-other-stuff
I want to transform the URL behind the scenes (meaning the URL won't change) to this:
www.example.com/product/?pid=[product-id]
I don't have much experience with htaccess and any help would be appreciated.
Here's the current htaccess file's content:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /downloads/www.bqwatches.com/
RewriteRule ^watch/([^-]+)- watch/?bqn=$1 [L,QSA,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /downloads/www.bqwatches.com/index.php [L]
</IfModule>
# END WordPress
Try this rule just below RewriteBase line of your regular wordpress .htaccess:
RewriteRule ^watch/([^-]+)- watch/?pid=$1 [L,QSA,NC]
Update: Altered the solutions to account for the fact that a product id consists of two letters, then a number.
You do the following:
RewriteRule ^product/([a-zA-Z][a-zA-Z][0-9]+).* product/?pid=$1
To explain what's happening, it matches the part after the URL that begins with product/, followed by two letters and several numbers, and then any other characters. The letters-and-numbers get put into the pid parameter of the requested URL, and the rest is discarded.
If you pass it http://www.example.com/product/af1234-some-product, it converts behind-the-scenes to http://www.example.com/product/?pid=af1234.
You could also accept any two characters followed by a number, by doing the following:
RewriteRule ^product/(..[0-9]+).* product/?pid=$1
If the product id could be any character that isn't a -, then the following should achieve the same:
RewriteRule ^product/([^-]+).* product/?pid=$1
Related
I have a custom page in wordpress that uses a custom plugin which is connecting to an API to retrieve information.
The client has asked that we don't pass id's via the url as below:
http://beta.hirehere.co.uk/hire-vehicle-detail/?id=4
but instead use a category name such as:
http://beta.hirehere.co.uk/hire-vehicle-detail/?vehicle_category_name=economy
initially I was using a URL rewrite to pretty things up so:
http://beta.hirehere.co.uk/hire-vehicle-detail/4
and thought I would just change it to:
http://beta.hirehere.co.uk/hire-vehicle-detail/economy
I would then adjust the code accordingly. I've done this yet the URL rewrite does not seem to be happy and returns a 404 error but it works if I access the direct url of:
http://beta.hirehere.co.uk/hire-vehicle-detail/?vehicle_category_name=economy
Here is my htaccess file, any help is greatly appreciated:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^hire-vehicle-detail/(.+)$ index.php/hire-vehicle-detail?vehicle_category_name=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
PS. The above htaccess code worked in the scenario of using ID's instead
Edit: it appears wordpress is trying to access a sub-page (economy) of the parent page (hire-vehicle-detail). I know this because if I create the page economy as a child of hire-vehicle-detail the url takes me there. Is there anyway to stop this
I have my wordpress installed on www.site.com/blog/ but i would like to add language codes in front of the blog directory (www.site.com/us/blog/, www.site.com/es/blog/ and so on). Is there a way achiving this? I need to keep those virtual directories inside URI so when visiting www.site.com/us/blog i want to show www.site.com/us/blog in the address bar.
This is my .htaccess file from www.site.com/blog/ directory.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress
RewriteEngine On
RewriteRule ^(.*)/blog/$ blog/?language=$1
it's been a while since i've written .htaccess, let me know how this works out.
edit: here's a helpful tutorial on how it works :)
https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
This should work for you:
RewriteEngine on
RewriteRule ^[a-z]{2}/(.*)$ /$1 [QSA,L]
It checks for any 2 letter prefix at the start of the URL, followed by anything else, then treats that as a request for the bit after, so /us/blog will be treated as a request for /blog, just as /es/page/123 would be treated as a request for /page/123. So long as a visitor goes to the prefixed URL, the address won't be redirected.
If you want to be more specific about what language prefixes to use, do this instead:
RewriteEngine on
RewriteRule ^(es|fr|us)/(.*)$ /$2 [QSA,L]
Making sure you add the ones you want between the first set of parantheses (and separated by pipes (|) as shown above.
In a WordPress website, all URLs with a trailing /iframe/ should be rewritten to ?iframe=1, e.g.:
mysite.com/page1/iframe/ should be rewritten as mysite.com/page1?iframe=1
For fun, I tried the following:
RewriteRule ^(.*)/iframe/$ http://microsoft.com [L]
Which is doing what I expected. The RegEx should be correct.
Next, I tried this:
RewriteRule ^(.*)/iframe/$ /$1?iframe=1
In context of WordPress' own rules, the complete file looks like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Here goes my line:
RewriteRule ^(.*)/iframe/$ /$1?iframe=1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
But, despite all my tries and variations, the only thing I constantly get, is a 404.
Where am I going wrong?
When you do the redirect to ?iframe=1, you didn't add the [R] flag, which means the URL will not change in the address bar. This in turn means that Wordpress will see "/iframe/" as part of the URL, and tries to find a page or post with that URL. It doesn't find one, so it gives you a 404.
So modify your rule like this:
RewriteRule ^(.*)/iframe/$ /$1?iframe=1 [R,L]
The L flag makes sure that the processing of the rules ends after this rule, then starts over again. This is required to make the R kick in and actually rewrite the URL before it gets to the Wordpress part.
This does mean that ?iframe=1 will remain visible in your URL.
I have been tearing my hair out trying to solve an issue with htaccess on wordpress network site and i've gained so much information from stackoverflow in the past that i thought this would be the best place to ask. And apologies up front if im not posting correctly, it my first time.
here are the specifics of my setup (i cannot show or allow access to the site as i have agreed to an NDA of sorts)
there are 2 sites. the first is the root site "/" and the second is "/mythoughts/" as shown in network admin.
the "/mythoughts/" site is a replacement for an old custom blog someone built that uses variables in the url (custom)
the themes i am using are twentyten and roots (obviously roots doesnt do tidy url rewrites as its on a network setup)
the problem is this.
first "index.php is being removed from the url (its not a problem, but i think it might cause problems when i try to do other rewrites.)
second, the old site has variables in the urls in 2 instances.
the first instance is this
www.thesite.com/mythoughts/index.php?year=2010&month=9
which i need to rewrite as
www.thesite.com/mythoughts/2010/9
first of all the index.php is automatically removed
i have tried so many different things like
RewriteRule ^mythoughts/index.php?year=(.*)&month=(.*)$ mythoughts/$1/$2 [R=301,L]
does not work
the second instance is this
www.thesite.com/mythoughts/index.php?thought=101
which should rewrite to
www.thesite.com/mythoughts/title-of-the-post
i have a script that can match the url variable to the title of the post (and replace remove illegal characters) but the rewrites just do not work.
here is the htaccess i currently have (a bare wordpress)
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
</IfModule>
#END WordPress
if anyone can help in pointing me in the right direction on this whole htaccess thing (which i have been struggling with for the last few weeks) i would be totally greatful.
thanks in advance guys and girls.
In the first instance, you have:
RewriteRule ^mythoughts/index.php?year=(.*)&month=(.*)$ mythoughts/$1/$2 [R=301,L]
But you can't match against the query string in a rewrite rule. Also, since you've got a rule to rewrite it back, you'll just create a redirect loop. You need to match against the actual request and not the URI.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /mythoughts/index\.php\?year=([^&]*)&month=([^&\ ]*)
RewriteRule ^ mythoughts/%1/%2 [R=301,L]
As for:
i have a script that can match the url variable to the title of the post (and replace remove illegal characters) but the rewrites just do not work.
I don't see any script in your htaccess, maybe if you include the rule or the script.
after continuing my search even though i posted the question on stack (the great) overflow
here is what i ended up with.
in the first instance with the double url string the answer was
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /mythoughts/index\.php\?year=([0-9\-]+)&month=([0-9\-]+)\ HTTP/ [NC]
RewriteRule ^index\.php$ http://site.co.uk/mythoughts/%1/%2? [R=301,L]
so the initial url would have been
www.site.co.uk/mythoughts/index.php?year=2010&month=12
and the rewrite result is
www.site.co.uk/mythoughts/2010/12/
PERFECT !!!!
in the second instance, first to answer Jons question.
the script is written with php, it scans through the old database concatin strings from the id of the url variable, as example the variable in the url below
www.site.co.uk/mythoughts/index.php?though=21
this post or database entry has a title, and in wordpress the title is used in the permalink
so it would be
www.site.co.uk/mythoughts/sublime-and-powerful-outcomes
the script takes the id for the first condition of the rewrite, and puts the id in its place. The second part takes the title of the post and swaps spaces with dashes and special characters removed.
which results in the new url.
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /mythoughts/index\.php\?thought=21
\ HTTP/ [NC]
RewriteRule ^index\.php$ http://site.co.uk/mythoughts/sublime-and-powerful-outcomes? [R=301,L]
thanks to Jon for taking the time to answer this.
hope this helps someone
I am working on a wordpress blog, with the default .htaccess settings:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
In my wordpress blog, I have created a page called "mypage". Using my rewrite settings, it is shown in the address bar as http://www.mysite.com/mypage.
I am using a custom page template and am processing some things, which include the use of GET variables. For instance:
if (isset($_GET='word')) { echo $_GET['word]; }
So, http://www.mysite.com/mypage?word=dog will display "dog".
The problem I am having is rewriting the URL so that it can look like: http://www.mysite.com/mypage/dog, still being able to access "dog" as the GET variable.
I am not too good with mod_rewrite rules to begin with, but working from within a wordpress installation is throwing me an extra curveball.
Does anyone know what I need to add to my .htaccess to achieve this?
Thank you!
Try the following:
RewriteRule ^mypage/([^/])/?$ mypage?word=$1 [L,QSA]
The L flag instructs .htaccess to stop processing rules, and QSA appends the query string since you might have other GET parameters on the end of the URL.
Mod_Rewrite Documentation