Extra clean Wordpress clean urls - php

I'm using wordpress for a site and I added an extra php page that uses a query string.
The url I currently have is site.com/page/?query=stuff
and I would like it to be site.com/page/stuff
I understand how to handle a .htaccess ReWrite rule for normal php projects but I'm having a hard time working around wordpresses .htaccess rules which are:
# 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 for the help.

So in any Wordpress install I've used it automatically identifies /search/query+terms the same as ?s=query+terms, so you really just need to redirect users to the more SEO version, in which case you just need to do the following:
Add the following just below RewriteBase /:
RewriteCond %{QUERY_STRING} s=(.*)
RewriteRule ^$ /search/%1? [L,R=301]
So your final file will look like this:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} s=(.*)
RewriteRule ^$ /search/%1? [L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Hope this helps! If your Wordpress install doesn't identify /search/query+terms as a search then let me know and I'll see if I can help you further! You can test this by manually going to the url before you try this, for example: example.com/search/hello+world

Related

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>

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!

How to rewrite the url by htaccess and also replace the underscore with dash

I have a WordPress site running in an apache server, I want to rewrite some the URL in SEO friendly manner, so how to do that
Currently, I have this URL
www.example.com/about-us/?id=what_we_do_1
www.example.com/about-us/?id=why_us
I want it like
www.example.com/about-us/what-we-do-1
www.example.com/about-us/why-us
and for this
www.example.com/media/?id=team
www.example.com/media/?id=abc_xyz
I want something like this
www.example.com/media/team
www.example.com/media/abc-xyz
my current .htaccess is like the below
# 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
To convert urls like
/index.php?p=test
to
/index.php/test
RewriteEngine On
RewriteCond %{QUERY_STRING} p=(.+)
RewriteRule ^/?index.php$ /index.php/%1? [R,L]
Thus in your case
RewriteEngine On
RewriteCond %{QUERY_STRING} p=(.+)
RewriteRule ^/?about-us?$ /about-us/%1? [R,L]
Similarly for other pages
You can try these to convert underscore to dash :-
RewriteEngine On
RewriteBase /
After that for another url :-
1) www.example.com/about-us/?id=what_we_do_1
htaccess rule will be :-
RewriteRule ^about-us/(.*)$ about-us/?id=$1 [QSA,L]
It may help you.

Enter own .htaccess rewrite rules in wordpress

I want to write a own rewrite rule into my htacces. The redirection itself works fine but Wordpress validates the URI with $_SERVER["REQUIRE_URI"]. So the site Title is Changed automatically to page not found, the loop doesn't works and so on.
Is there anyway to do this?
If not is there a other possibility actually I only need a really simple redirect from "www.example.com/test/" to "www.example.com/t.php?a=1" So it's completely static i don't need patterns at all. I have already tried it with "frameset" but that looks pretty ugly and makes the process slow.
Thank you very much
Jakob
My .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# My Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^Malta/$ http://www.reisekontrast.de/Index?malta=1 [QSA,L]
RewriteRule ^China/$ http://www.reisekontrast.de/Index?china=1 [QSA,L]
# WP Rules
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
We need your original .htaccess file. This is an easy rewrite (redirect), but wordpress already has URL Rewriting rules. To help you best would mean we need to work cohesively with what settings you already have running. Otherwise your are just going to break Wordpress but get your URL's working.
Also, as a primer question, does your server support full override in htaccess?
Based on your HTACCESS file, I would add the section for "Custom" Rules. That way it bypasses the Rewrite Conditions that are arbitrary in this type of rewrite.
Hope this helps! Let me know if I can provide more detail or other options.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Custom rules
RewriteRule /test/(.*) http://example.com/t.php?a=1 [NC]
# My Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^Malta/$ http://www.reisekontrast.de/Index?malta=1 [QSA,L]
RewriteRule ^China/$ http://www.reisekontrast.de/Index?china=1 [QSA,L]
# WP Rules
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /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