Rewrite GET Variable in URL - .htaccess

I am using WordPress's permalink structure %category%/%postname%/ with the following htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The url's take the form http://www.example.com/category/postname/. I have a plugin where if you append ?m=gallery to the URL it will bring you to the posts' gallery. So http://www.example.com/category/postname/?m=gallery brings you to the gallery. I would like to be able to use http://www.example.com/category/postname/gallery/ instead. What do I need to modify in my htaccss to achieve my desired results? I would imagine it would be something like this RewriteRule ^(.+)/gallery$ $1?m=gallery
Edit - Current htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.+)/gallery/$ $1/?m=gallery [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Considering URL example you have provided (http://www.example.com/category/postname/gallery/ => http://www.example.com/category/postname/?m=gallery) you will need this sort of line:
RewriteRule ^(.+)/gallery/$ $1/?m=gallery [QSA]
Added missing slashes /
Added [QSA] flag (may not be really necessary actually).
You would need to put this rule somewhere on the top: after this line, for example: RewriteRule ^index\.php$ - [L]
Obviously, you ensure that WordPress will generate this kind of URLs: http://www.example.com/category/postname/gallery/

Related

Remove Trailing Slash From URLs

I'm trying to remove the trailing slash from our URLs. I adjusted our .htaccess file and added this code:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
Unfortunately this didn't work as the pages (except for the homepage) suddenly show too many redirects and I get a message from the system saying "The page isn’t redirecting properly".
I was thinking maybe I made an error in the way I inserted the code. Originally, our .htaccess file has 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>
# END WordPress
# 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
After adjusting it to insert the code, it looked like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# BEGIN WordPress
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
And that resulted in an error. Can anybody point out where I went wrong? Thanks.
Turns out it's a Wordpress issue. It's something you can adjust by going to your Wordpress site's Permalink Settings. From here you can choose your preferred URL format. "With-trailing-slash" is the default format so you definitely need to make an adjustment if you want the typical "without-trailing-slash" format.

htaccess rewrite to specific url does not work

I try to rewrite all languages to /en/ on a site. But I do something wrong and do not know what.
All domain request like https://www.shop.com/de/ or https://www.shop.com/ must rewrite to https://www.shop.com/en/
Why I need this? First my shop must be ready in /en/ language. When everything works fine I will add /de/ and then /nl/.
Does someone have a solution for me? Would be very nice.
Here the code I use in htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule https://www.shop.com/de/ https://www.shop.com/en/
RewriteRule https://www.shop.com/nl/ https://www.shop.com/en/
RewriteRule https://www.shop.com https://www.shop.com/en/
RewriteRule https://www.shop.com/ https://www.shop.com/en/
</IfModule>
You cannot match full URLs in RewriteRule.
You can use these rules:
RewriteEngine On
RewriteBase /
RewriteRule ^(de|nl)?/?$ /en/ [L,NC,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

URL rewrite via htaccess

I've to change this url : http://localhost:8888/finance/schedule/?location=new_york
to http://localhost:8888/finance/schedule/location/new_york
It's a wordpress site, my htaccess code is :
RewriteRule ^schedule/location/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\$ schedule/?location=$1
But nothing seems to happen, please suggest.
The whole htaccess file code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /finance/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /finance/index.php [L]
RewriteRule ^schedule/location/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)\$ schedule/?location=$1
</IfModule>
This line is stopping it from ever reaching your later rule:
RewriteRule . /finance/index.php [L]
The [L] means "last," as in "this is the last rule to run if it matches."
And \$ looks for a literal $, not the end of the string.
You also have two many matching groups in your rule.
You need something like this, instead (totally untested):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /finance/
RewriteRule ^index\.php$ - [L]
RewriteRule ^schedule/location/([a-zA-Z0-9_-]+?)/?$ /wp-content/themes/financeinstitute/page_schedule.php/?location=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /finance/index.php [L,QSA]
</IfModule>

htaccess rewrite rule blog links

I have been using htaccess to remove rewrite certain URLs, however I am looking at something a little more complicated at the moment.
Our website blog (WordPress) used to have links like this:
​/blog​/postname/1387
However after redoing the website our links are now currently just
/postname
Would it be possible to redirect any uses from ​/blog​/postname/1387 and get rid of the blog and number at the end via htaccess so it just contains the postname? At the moment I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^blog/(.*)/.*$ $1/
</IfModule>
Would love to hear any hints or tips, as this is not actually doing any redirecting, what am I doing wrong?
Let's just do a little cleanup:
<IfModule mod_rewrite.c>
#Turn on the RewriteEngine
RewriteEngine On
#Rewrites are all relative to /
RewriteBase /
#Explicit - If the request is for index.php, do nothing.
RewriteRule ^index\.php$ - [L]
#Conditional – Unless the file or directory specifically exists,
#If the request is for the old style blog URI, redirect to new style and stop.
#Otherwise, redirect to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/(.*)/.*$ $1/ [R=301,L,QSA]
RewriteRule . /index.php [L]
</IfModule>

Remove ? with htaccess

I have a Wordpress installation that works and a 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
Outside Wordpress I have created links like this:
http://www.test.com/butik/?my-url
I think it looks nice except for the "?". Is it simple to add something to my htaccess-file to remove it?
http://www.test.com/butik/my-url/ would be perfect. Does it matter if I run a file at the path http://www.test.com/butik/index.php? Would it collide?
If I'm understanding correctly, you can try the following:
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule (.*) $1%1? [L]
So, given your example, $1 would be /butik/ and %1 would be my-url (captured from the RewriteCond) Also, the first RewriteCond should prevent any conflicts with index.php requests.
Hope this helps.

Resources