Rewrite URL to hide query string - php

I'm trying to mask the query string of my pages in order to hide it's unique page ID. This is important as each ID needs to be unique to a user.
Currently the URL structure looks like this:
http://domain.com/page.php?Page_ID=1234
(where 1234 is any number)
but I need it to look like this:
http://domain.com/page.php
I have tried adding the following to the .htaccess file but it does not seem to make any difference:
RewriteEngine on
RewriteBase /
RewriteRule ^page.php?Page_ID=([0-9]+)/$ page.php [L,QSA,NC]
I've looked at other posts like this one and others, but can't seem to find a solution. Is there something I might be missing here?

Query string is not part of match in rewrite rule, you need to match against %{THE_REQUEST} using a rewriteCond
RewriteEngine on
RewriteCond %{THE_REQUEST} /page\.php\?page_ID=.+ [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]

Related

Search replace query string with htaccess

I have my site url like this.
https://example.com/page/1/?orderby=low_to_high&s=demo+post&post_type=post&twdg_wsac=1
I want to replace some part of this url using htaccess so the final output should be like this
https://example.com/page/1/?orderby=low_to_high&s=demo+post&post_type=post&custom_posts=1
So I have made changes in the htaccess file like this
RewriteEngine on
RewriteRule ^&twdg_wsac$ &custom_posts
But its not working. Also as you can see in the url there is twdg_wsac=1. So the last "1" is a pagination for post so that would change dynamically as per posts count.
So can someone tell me how to do this?
Any help and suggestions would be really helpful. Thanks
Update
All the parameters in the url are dynamically generated I mean those are the filter parameters. So they cant be same except twdg_wsac
Try with below, we are doing rewrite on query string.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^orderby=(.+)&s=(.+)&post_type=(.+)&twdg_wsac=([\d]+)$
RewriteRule ^ %{REQUEST_URI}?orderby=%1&s=%2&post_type=%3&custom_posts=%4
RewriteEngine On
RewriteCond %{QUERY_STRING} twdg_wsac=([\d]+)$
RewriteRule ^ %{REQUEST_URI}?orderby=low_to_high&s=demo+post&post_type=post&custom_posts=%1 [L]

Rewrite Dynamic URL In PHP Using .htaccess

I wanted to rewrite url in following pattern
My current url is:
xyz.com/news_details.php?id=120&title=hello-world
and I want the url as is:
xyz.com/hello-world
also I wanted to fetch the values of id on this page like:
$id = $_REQUEST['id'];
please help
I did use the following code:
RewriteCond %{THE_REQUEST} /news_details.php\?id=([^&\s]) [NC]
RewriteRule ^ /%2\.html? [NC,R,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-])\.html$ /news_details.php?id=$2 [QSA,L,NC]
You could do something like this :
RewriteEngine On
RewriteRule ^/(.*)/(.*)$ /news_details.php?title=$1&id=$2
To achieve links like http://example.net/hello-world/120.
The ^/(.*)/(.*)$ part tells apache the pattern that it will follow. The second part (news_details.php?title=$1&id=$2) gives apache the real link that it will apply the pattern on.
You can't exactly hide arguments from the user, because you need a way to get the id that you want to have as the id argument. So that's the only possible way to do that.
You can read more about it here.

Url rewrite [Long url to short]

I'm developing CMS and want to make template system. So I could have multiple themes, like Wordpress has. But I have problem with urls.
My question is how to rewrite this url:
http://example.com/themes/mytheme/post.php?slug=some-post-title
to something like this:
http://example.com/post/some-post-title
So point is to cut that part of themes/mytheme
Try this:
RewriteEngine on
RewriteCond %{QUERY_STRING} slug=(.*)
RewriteRule ^([^/]*)/([^/]*)/post.php post/%1? [NC]
Tested here.
The RewriteCond detects the query string and allows for a back-substitution with %1 in the RewriteRule.
^([^/]*)/([^/]*)/ matches the first two folders.
The ? at the end of the RewriteRule is an empty query string so that the GET variables won't be passed through.
[NC] means case insensitive comparisons.

Want .htaccess to display / insteade of ? in URL and also i am able to get data like $_GET["param1"]

My Actual URL is below.
http://localhost/waterpump/index.php?param1=4498&param2=930&param3=876&param4=201&param5=vis
But my client want in below format.
http://localhost/waterpump/param1/4498/param2/930/param3/876/param4/201/param5/vis
And also i am able to get data using $_GET["param1"]
How can I do this through .htaccess?
Try examples from htaccess tricks
e.g.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^blog/([0-9]+)-([a-z]+) http://corz.org/blog/index.php?archive=$1-$2 [NC]
I am not sure if the number of parameters if fixed or variable but you would do essentially a matching part, where you match the url separated by slashes and then you would "rewrite" it to the original url
If you are set on using $_GET to get the parameters and there will always be between one and five parameters then you could use the following rewrite:
RewriteEngine On
RewriteRule ^waterpump/param1/(.*)/param2/(.*)/param3/(.*)/param4/(.*)/param5/(.*)/ /index.php?param1=$1&param2=$2&param3=$3&param4=$4&param5=$5 [nc]
RewriteRule ^waterpump/param1/(.*)/param2/(.*)/param3/(.*)/param4/(.*)/ /index.php?param1=$1&param2=$2&param3=$3&param4=$4 [nc]
RewriteRule ^waterpump/param1/(.*)/param2/(.*)/param3/(.*)/ /index.php?param1=$1&param2=$2&param3=$3
RewriteRule ^waterpump/param1/(.*)/param2/(.*)/ /index.php?param1=$1&param2=$2 [nc]
RewriteRule ^waterpump/param1/(.*)/ /index.php?param1=$1 [nc]
Editted
If you have implemented this correctly doing a print_r($_GET); on /waterpump/param1/x/param2/y/param3/z/ Should give you something like:
array (
'param1'=>x,
'param2'=>y,
'param3'=>z,
);
There are cleaner ways of doing this that would involve less rewrites, but some changes to your PHP. Also the method above will only work for waterpumps. Will there be anything other than water pumps?

Mod rewrite user URL

I'm lost here. I'm using this script to give users the opportunity to enter their username lijke this:domain/username
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ userpage.php?user=$1 [NC,L]
</IfModule>
This works fine. However, every user has pages I must link to: Video, Music, Images etc...
So I need something like:
domain/username/video
In php code it must be something like:
user.php?user=test&page=video
And one other question: What is the preferable way to link in this situation?
userpage.php?user=test&page=video
or
/test/video
And finally: Is it possible to deny the possibility to enter the url:
domain/userpage.php?user=test&page=video? Instead just always show: domain/test/video
Thanks in advance
I'm not 100% sure what you're asking? Do you need to change the rewrite rule to match the URL site.com/moonwalker/videos? You could try this:
RewriteRule ^([^/]+)/(images|videos|music)/?$ userpage.php?user=$1&page=$2 [NC,L]
Update
Just a quick note on the domain/member/videos URL structure. That could end up causing you problems in the future. For instance what if you decide to have a single page that shows all member videos? You'd probably want to URL to look something like site.com/members/videos. That's a problem, because the rewrite rule will also match that, but "members" isn't a member username.
I would probably structure my member page URLs like site.com/user/moonwalker/videos so it doesn't clash with future rewrite rules. You would change the above rewrite rule to this:
RewriteRule ^user/([^/]+)/(images|videos|music)/?$ userpage.php?user=$1&page=$2 [NC,L]
Then later on you can add a rewrite rule like:
RewriteRule ^members/(images|videos|music)/?$ allusers.php?page=$1 [NC,L]
To show all member videos.
Yes, it is possible by looking at the request line:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /userpage\.php[?\ ]
RewriteRule ^userpage\.php$ - [F]
This is necessary as the URL path could already be rewritten by another rule and thus using just RewriteRule would match those already rewritten requests too.

Categories