url rewrite in php for change posts url - apache

I want change url:
http://example.com/posts.php?action=view&id="a number"
to
http://example.com/posts/"a number"
I write these into the .htaccess file but they don't work:
RewriteEngine On
RewriteRule ^posts/([0-9]+)/?$ posts.php?action=view&id=$1 [NC,L]
RewriteRule ^posts/?$ posts.php [NC,L]

It'll be like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /(posts)\.php\?action=view&id=(\d+) [NC]
RewriteRule ^posts\.php$ /%1/%2? [R=301,L,NC]
RewriteCond %{THE_REQUEST} ^GET\ /(posts)\.php\ HTTP [NC]
RewriteRule ^posts\.php$ /%1/? [R=301,L,NC]
RewriteRule ^(posts)/(\d+)/?$ /$1.php?action=view&id=$2 [L]
RewriteRule ^(posts)/?$ /$1.php [L]

You can try below in your .htaccess file
.htaccess file code:
RewriteEngine On
RewriteRule ^posts/([0-9]+) /posts.php?action=view&id=$1 [QSA,L]
QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite.
L means if the rule matches, don't process any more RewriteRules below this one.

Related

htaccess rewrite if url doesnt have a query string

I have a htaccess that rewrites to /login if conditions aren't met.
It's working fine for urls without query_string.
However I have no clue how to include /reset?query_string to accepted conditions. And want to exclude /reset
/reset?6tdsBMxpeeJEDUvYzwKmLzIusLYLjgMtIj
RewriteEngine On
RewriteBase /index.php
RewriteCond %{REQUEST_URI} !^/login$
RewriteCond %{REQUEST_URI} !^/forgotten$
RewriteRule ^([^\.]+)$ /login [R=301,L,QSD]
RewriteRule ^(/)?$ /login [R=301,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Here is a modified version of your .htaccess that excludes /reset?query:
RewriteEngine On
# /reset with query string
RewriteCond %{QUERY_STRING} .
RewriteRule ^(reset)/?$ $1.php [L,NC]
RewriteCond %{REQUEST_URI} !^/(forgotten|login)$ [NC]
RewriteRule ^([^.]*)$ /login [R=301,L,QSD]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)$ $1.php [L]
Make sure to test it after clearing your browser cache.
With your shown samples and attempts, please try following htaccess rules file. Make sure to clear your browser cache before testing your URLs.
Also I am making use of apache's variable named THE_REQUEST by which we can handle both uri as well as query string.
RewriteEngine ON
RewriteBase /
##Rules for handling rest uri without query string.
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^reset/?$ /login? [R=301,L,NC]
##Rules for reset with uri and query string.
RewriteCond %{THE_REQUEST} \s/reset\?\S+\s [NC]
RewriteRule ^ /reset? [R=301,NC]
##Rule for login page's backend rewrite.
RewriteRule ^login/?$ login.php [QSA,NC,L]
##Rule for forgotten page's backend rewrite.
RewriteRule ^forgotten/?$ forgotten.php [QSA,NC,L]
##Rules for non-existing pages should be served with index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

htaccess mod_rewrite - Trailing Slash and loop of redirects

Here is my htaccess code:
RewriteEngine On
RewriteRule ^s/(.*)/(.*) /index.php?search=$1&category=$2 [L,QSA]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)&category=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/%2/? [R=301,L]
I need to make a rewrite rule like this:
http://mywebsite.com/s/query+term => http://mywebsite.com/?search=query+term
OR if there is a category
http://mywebsite.com/s/query+term/Category => http://mywebsite.com/?search=query+term&category=Category
I need also to redirect all the old urls to the new one.
With this rules all that I can obtain is to have the search term and category name joined togheter. In a nutshell it is as if I had always:
http://mywebsite.com/?search=query+term/Category
If I remove all the conditions (RewriteCond), leaving only the rewrite rule with the addition of a trailing slash:
RewriteEngine On
RewriteRule ^s/(.*)/(.*)/ /index.php?search=$1&category=$2 [L,QSA]
Reaching the url of my interest in a direct way, the whole thing works. But I will not have the redirect..
So I have tried to set a rule like this:
RewriteEngine On
RewriteRule ^s/(.*)/(.*)/ /index.php?search=$1&category=$2 [L,QSA]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^search=([A-Za-z0-9\+]+)&category=([A-Za-z0-9\+]+)$
RewriteRule ^(.*)$ /s/%1/%2/? [R=301,L]
But with this rule I get just a loop of redirects.
You can use the following rules :
RewriteEngine on
#1--Redirect from "?search=foobar&cat=cat" to "/s/foobar/cat" --#
RewriteCond %{THE_REQUEST} /\?search=([^&]+)&cat=([^\s]+) [NC]
RewriteRule ^ /s/%1/%/%2? [NC,L,R]
#2--Redirect from "/?search=foobar" to "/s/foobar" --#
RewriteCond %{THE_REQUEST} /\?search=([^/\s]+) [NC]
RewriteRule ^ /s/%1? [NC,L,R]
#1--Rewrite "s/foobar/cat" to "/?search=foobar&cat=cat"--#
RewriteRule ^s/([^/]+)/([^/]+)/?$ /?search=$1&$category=$2 [NC,L]
#2--Rewrite "s/foobar" to "/?search=foobar"--#
RewriteRule ^s/([^/]+)/?$ /?search=$1 [NC,L]

Url rewrite with underline automaticly

I'm trying to rewrite url automatically from
www.example.com/test.php?title=123jane
to
www.example.com/test_123jane
I'm using
RewriteRule ^test_([A-Za-z0-9-]+)/?$ test.php?title=$1 [NC,L]'
But this doesn't change it automatically.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /test\.php\?title=([a-z0-9]+) [NC]
RewriteRule ^test\.php$ /test_%1? [R=301,L,NC]
RewriteRule ^test_([a-z0-9]+)$ /test.php?title=$1 [L]
The above should work.

Get utl parameters / URL rewriting

I'd like to use URL rewriting to have "virtual" subdomains. I already created wildcard DNS entry.
I want subdomain to be added as GET parameter.
Example:
http://mysubdomain.domain.com/index.php
would call:
http://www.domain.com/index.php?subdomain=mysubdomain
and
http://mysubdomain.domain.com/page.php?parameter1=parameter
would call:
http://www.domain.com/page.php?parameter1=parameter&subdomain=mysubdomain
This is my .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+).domain.com$ [NC]
RewriteRule ^$ /index.php?subdomain=%2 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+).domain.com$ [NC]
RewriteRule ^([A-Za-z0-9_.]+)$ /$1 [L]
It works but I cannot get subdomain value, when calling other page than index.php, if I call http://subdomain.domain.com/page.php I loose subdomain parameter, I tried:
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+).domain.com$ [NC]
RewriteRule ^([A-Za-z0-9_.]+)$ /$1&subdomain=%2 [L]
But I got Internal error.
THis rule:
RewriteRule ^$ /index.php?subdomain=%2 [L]
needs a QSA flag:
RewriteRule ^$ /index.php?subdomain=%2 [L,QSA]
and this rule:
RewriteRule ^([A-Za-z0-9_.]+)$ /$1 [L]
needs the QSA flag and the proper backreference:
RewriteRule ^([A-Za-z0-9_.]+)$ /$1?subdomain=%2 [L,QSA]

.htaccess Rewrite GET-Data into GET-Data

i want to rewrite http://192.168.2.101/game.php?village=567919&screen=overview into http://192.168.2.101/index.php?dir=game&page=overview&village=567919 can anyone tell my why this is not working well and how i do it right?
my .htaccess code
RewriteEngine On
RewriteRule ^$ index/
RewriteRule ^(.*)/$ index.php?dir=main&page=$1 [L,QSA]
RewriteRule ^game.php?village=(.*)&screen=(.*)$ index.php?dir=game&page=$2&village=$1 [L,QSA]
You have to use %{QUERY_STRING} to parse query string parameters.
Replace your code by this one in your htaccess
RewriteEngine on
RewriteCond %{QUERY_STRING} ^village=([^&]+)&screen=([^&]+)$
RewriteRule ^game\.php$ /index.php?dir=game&page=%2&village=%1 [L]
RewriteRule ^(.+)/$ /index.php?dir=main&page=$1 [L]
RewriteRule ^$ /index.php [L]