.htaccess rule to rewrite query string to a path - php

I'm looking for a way to have the URL
site.com/collection/name
serve the content that is actually located at...
site.com/collection/?collection_name=name
The website is running on WordPress, and is currently using the default .htaccess rules located in the 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>
# END WordPress
Because it's on WordPress, it's currently looking for a page /collection/name that doesn't exist, and produces a 404 error.

There should be an index file under the folder "name"
If so, you can try this:
RewriteEngine On
RewriteBase /
RewriteRule ^/$ /collection/name/index.php
RewriteRule ^([a-zA-Z0-9_-]+)/$ /collection/?collection_name=$1
This is a generic rule that works in many similar occasions. Modify it to suit your website...

Related

Extract URL for Redirect

I have setup a website with recipes but in the process of migration OLD urls to NEW urls I'm facing issues with.
https://example.com/recipes/cheesy-baked-tacos/
This is my old URL structure and In a wordpress site I need to extract last part of the URL "cheesy-baked-tacos" and handle that in the template I use for recipes page.
Now when i click on the URL it goes to 404 page even though recipes page has setup.
I tried following htaccess rules but have no success
RewriteBase /
RewriteRule ^recipes/(.*)$ index.php?l=$1
Thanks in Advance.
Full htaccess file looks like this
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^recipes/(.*)$ index.php?l=$1
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
you should check your permalink change to starts with id to without query string
dont change htacces file
you just change permalink in new server
https://example.com/recipes/cheesy-baked-tacos/
this is your old url and has permalink set without query string
new url https://example.com/recipes/cheesy-baked-tacos/id=50 just like
thats why your code have error
Managed to fix the issue with 301 Redirect. Here's what i used if it helps someone.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^recipes/(.*)$ recipe/?post_name=$1 [L,R=301,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Redirection of website-want to use /en folder being redirected to post

I encountered a strange behaviour with WordPress website.
I have created /en folder in the root and want to have another WordPress website there.
The problems appears when I try to open mainwebsite.com/en I'm being redirected to mainwebsite.com/english-post
So, WP gives the priority to the post that is found in the main domain and it's slug begins with "en".
I tried changing the folders name to "ren" and the website in the folder than shows up nicely.
I've checked .htaccess file for some redirects and none is set.
I noticed that this is a typical WP behaviour, as I tested on some "bigger" brands that use WP and same thing happens,ie:
https://www.smashingmagazine.com/en redirects to --> https://www.smashingmagazine.com/2014/11/enabling-multiscreen-tracking-with-google-analytics/
http://vanheusen.com/en redirects to --> http://vanheusen.com/products/english_shaded_box_silk_tie/
Have any idea how to solve this?
This is my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(en/)
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Try using this rule in your main .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php - [L]
RewriteCond %{REQUEST_URI} !^/en
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
And then you need this rule below in the .htaccess inside en folder
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /en/
RewriteRule ^index\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /en/index.php [L]
</IfModule>
# END WordPress
Let me know how this works

htaccess file has no affect

I'm trying to rewrite an url like this:
www.mywebsite.com/product/a-product-name-here/92341
to
www.mywebsite.com/our-products/a-product-name-here/92341
I'm using following rewriterule:
RewriteRule ^product/(.*)$ our-products/$ [R=301,L]
And according to this online tool it should do the trick. I've put this online, but it didn't affect anything.
AllowOverride is active on my host.
My folder structure is like this (tried the rewrite rule in both of the 2 .htaccess's):
UPDATE
This is the .htaccess content in the folder Corporate
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /corporate/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /corporate/index.php [L]
</IfModule>
# END WordPress
And this is the content of the .htaccess file in the product folder:
AddDefaultCharset utf-8
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.+) index.php?a=$1 [nc]
Alright, as far as I understand, your problem can be divided into two parts.
Redirect public URL
You want the public URL example.com/product/foo-bar/1234 to be redirected to example.com/our-products/foo-bar/1234. This can be achieved with the already posted rule by anubhava, slightly modified to only match numbers:
Put it in your root .htaccess, besides index.php.
RewriteRule ^product/((.+)/(\d+))$ /our-products/$1 [R=301,L]
Map external URL to script, internally
Then, the external URL is example.com/our-products/foo-bar/1234, which does not physically exist on the server machine and therefore fails with a 404.
So you'll have to map it to the script containing the actual logic.
RewriteRule ^our-products/(.+)$ /product/index.php?a=$1 [L]
This is assuming that your script will accept the a parameter.
Your .htaccess files, after the changes
This is the .htaccess content in the folder Corporate
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^product/((.+)/(\d+))$ /our-products/$1 [R=301,L]
RewriteRule ^our-products/(.+)$ /product/index.php?a=$1 [L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /corporate/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /corporate/index.php [L]
</IfModule>
# END WordPress
And this is the content of the .htaccess file in the product folder:
AddDefaultCharset utf-8
Options +FollowSymlinks

Using .htaccess to rewrite urls with ID: "Page not found"

I am trying to rewrite some urls by using the .htaccess file in a WordPress environment. I have a WordPress page called "offerta" with two parameters, one of them is a custom ID that I use to generate content.
so, an example url is: http://www.riccionelastminute.it/rlm/offerta/?id=15&titolo=my-title
and I would like it to be: http://www.riccionelastminute.it/rlm/offerta/my-title-15/
this is my .htaccess right now:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /rlm/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /rlm/index.php [L]
</IfModule>
# END WordPress
After some research, I tried to add this rule right after the last Rewrite Rule, but it redirects to a 404 page:
RewriteRule ^offerta/([A-Za-z0-9-]+)-([0-9]+)/$ offerta/?id=$2&titolo=$1
I'm not used to play around .htaccess and I'm not an expert.
Am I missing something?
The problem is that the last RewriteRule has the L flag which means that no other rules are applied after it. What you need to do is add your rule before the last one:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /rlm/
RewriteRule ^index\.php$ - [L]
RewriteRule ^offerta/([A-Za-z0-9-]+)-([0-9]+)/$ offerta/?id=$2&titolo=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /rlm/index.php [L]
</IfModule>

HTAccess URL Rewrite affect GET variables

We’re using wordpress, and we have the following in the .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
We have a URL like http://subdomain.example.com/select-a-card/?q=fuel, and we want to rewrite it so that it becomes something like http://subdomain.example.com/fuel-card/
What do I need to add to the htaccess to do this? Also, will this affect the queries from running that uses GET variables?
nothing, just read the wordpress manual and edit your settings. checkout http://codex.wordpress.org/Using_Permalinks
--- EDIT
This can be done, but only if you have some sort of a standard markup of the url's you want to have rewritten. Ie. if you only want to rewrite the select-a-card url's, or only the url's with a syntax like http://site.com/uri/?q=something. But you if the syntax of the url's differ too much you will have to add a lot of rewrites.
In this specific case something like this should work:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# add your specifics below:
RewriteRule ^/select-a-card/\?q=(.*) $1-card/
RewriteRule . /index.php [L]
</IfModule>

Categories