0

How to change/translate query strings with .htaccess or using wp_rewrite()?

First example

Current URL is:

www.website.com/post-name/?edit= post-id

I want it to be:

www.website.com/post-name/edit/post-id

Second example

Current URL is:

www.website.com/post-name ?updated=true

I want it to be:

www.website.com/post-name /updated

Third example - WP "default" ancors

Current URL is:

www.website.com/blog-post-name/ #more- post-id

I want it to be:

www.website.com/blog-post-name/


I've literally looked 50 posts, tutorials and articles about this subject & I can't get it to work.

One basic example I've tried with .htaccess:

<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule ^edit/([^/.]+)/?$ ?edit=$1 [L]
</ifModule>

Could someone write me an example based on one or more of my examples which I could use to work my way up?

About 3rd example - I can't find this anchor from any of my templates, is there a way to change/translate it without finding it and hard-coding it?

AWA
  • 436
  • 4
  • 16
  • I don't think #more will ever reach the server. # denotes an anchor that the browser uses client side. it is never sent. for the rest, I'm working on it – Leo128 Aug 26 '15 at 17:37
  • Just what I thought. I was also hoping to get answer, how to translate or change WordPress "default" anchors but I must forgot. I'll update the question. – AWA Aug 26 '15 at 17:45

1 Answers1

2

the following seems to work for the first and second case

RewriteEngine on

RewriteCond %{QUERY_STRING} edit=(.*)
RewriteRule (.*)/ $1/edit/%1? [NE,L]

RewriteCond %{QUERY_STRING} updated=true
RewriteRule (.*)/ $1/updated? [NE,L]

As I stated in the comment above, # anchors are never sent to the server.

Also my solutions are roughtly tested in this sandbox, so take your time to verify they really work for you.

reference: apache wiki

Update: I tested on a real box, it kinda works.

my test input was http://www.myawesomesite.com/awesome-post-001/?edit=awe001 (btw: I don't own that domain, I edited /etc/hosts to fake it)

here is my conf

    RewriteEngine on

    RewriteCond %{QUERY_STRING} edit=(.*)
    RewriteRule (.*)/ $1/edit/%1/? [NE,L]

    RewriteCond %{QUERY_STRING} updated=true
    RewriteRule (.*)/ $1/updated/? [NE,L]

    LogLevel alert rewrite:trace6

Here is an excerpt from my mod_rewrite trace log.

strip per-dir prefix: /var/www/html/awesome-post-001/ -> awesome-post-001/
applying pattern '(.*)/' to uri 'awesome-post-001/'
RewriteCond: input='edit=awe001' pattern='edit=(.*)' => matched
rewrite 'awesome-post-001/' -> 'awesome-post-001/edit/awe001/?'
split uri=awesome-post-001/edit/awe001/? -> uri=awesome-post-001/edit/awe001/, args=<none>
add per-dir prefix: awesome-post-001/edit/awe001/ -> /var/www/html/awesome-post-001/edit/awe001/
strip document_root prefix: /var/www/html/awesome-post-001/edit/awe001/ -> /awesome-post-001/edit/awe001/
internal redirect with /awesome-post-001/edit/awe001/ [INTERNAL REDIRECT]
strip per-dir prefix: /var/www/html/awesome-post-001/index.html -> awesome-post-001/index.html
applying pattern '(.*)/' to uri 'awesome-post-001/index.html'
RewriteCond: input='' pattern='edit=(.*)' => not-matched
strip per-dir prefix: /var/www/html/awesome-post-001/index.html -> awesome-post-001/index.html
[applying pattern '(.*)/' to uri 'awesome-post-001/index.html'
RewriteCond: input='' pattern='updated=true' => not-matched
pass through /var/www/html/awesome-post-001/index.html
strip per-dir prefix: /var/www/html/awesome-post-001/edit/awe001/ -> awesome-post-001/edit/awe001/
applying pattern '(.*)/' to uri 'awesome-post-001/edit/awe001/'
RewriteCond: input='' pattern='edit=(.*)' => not-matched
strip per-dir prefix: /var/www/html/awesome-post-001/edit/awe001/ -> awesome-post-001/edit/awe001/
applying pattern '(.*)/' to uri 'awesome-post-001/edit/awe001/'
RewriteCond: input='' pattern='updated=true' => not-matched
pass through /var/www/html/awesome-post-001/edit/awe001/
strip per-dir prefix: /var/www/html/awesome-post-001/edit/awe001/index.html -> awesome-post-001/edit/awe001/index.html
applying pattern '(.*)/' to uri 'awesome-post-001/edit/awe001/index.html'
RewriteCond: input='' pattern='edit=(.*)' => not-matched
strip per-dir prefix: /var/www/html/awesome-post-001/edit/awe001/index.html -> awesome-post-001/edit/awe001/index.html
applying pattern '(.*)/' to uri 'awesome-post-001/edit/awe001/index.html'
RewriteCond: input='' pattern='updated=true' => not-matched
pass through /var/www/html/awesome-post-001/edit/awe001/index.html

btw, are you sure you could not solve your problem using mod_alias? sometimes decoding which one is best is non-trivial, just asking.

Leo128
  • 163
  • 12
  • Hmm, I'll get no 500 or any other error but it doesn't change query string, it also gives 404 if you insert it manually. Could that be affecting if my url structure is www.my-site.com/page/?edit=post-id? Note that I have front-end page where you can submit/edit page. Update also adds up like that: ?edit=post-id&updated=true – AWA Aug 26 '15 at 18:36
  • I am sorry I posted the wrong link to the mod_rewrite tester. now it is okay. Try http://htaccess.madewithlove.be/. I am sorry but I have no other means to test at the monent. I'll update if I get a clue – Leo128 Aug 26 '15 at 18:42
  • That's very odd, tester changes it successfully but it doesn't apply in "real" situation. Does **https** changes anything? – AWA Aug 26 '15 at 18:56
  • https should not interfere, except that you have to put your mod_rewrite rules in the proper apache .conf file which usually differs from the one used for http. – Leo128 Aug 26 '15 at 20:23
  • I tested it on a real apache 2.4 installation on ubuntu 14.04, it works indeed. maybe the problem is not with the rules. could you turn rewrite_log on and post your results please? follow this post http://stackoverflow.com/questions/9632852/how-to-debug-apache-mod-rewrite . Please note that you cannot do that in .htaccess – Leo128 Aug 26 '15 at 21:10
  • Im working on it but I can't locate any config files. Any idea what it might be called? I've got Apache 2.4.10 and FreeBSD OP. Docs say it's httpd.conf but I can't see it and I don't know nothing about servers. Maybe host has disabled/hid main config files. – AWA Aug 26 '15 at 23:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88052/discussion-between-leo128-and-awa). – Leo128 Aug 26 '15 at 23:01