I have a id parameter in the url and it looks like
http://localhost/project/index.php?id=20234532
The problem is this number can be grown up to a bigger number within few days.
First thought : If there is any way to remove the id value from url using htaccess then that's fine.Not sure though.
Second thought : If the value 20234532 can be encoded in a shorter value and then decode again.
I can't think of any other possible idea now.Also I can't use post method as this id will come in the url always.
Apologies, if I put the question in wrong place as clearly it is not an issue rather seeking for solution.
A number of things you can do here. You can convert it to base 32, and make the URL even short by using mod_rewrite:
$id = (whatever comes out of database)
$shortID = base_convert($id,10,32);
Then use the $shortID to make the URL: http://example.com/$shortID, so maybe something that looks like http://example.com/a5pqnf3k (that's ID = 349832985716).
Then in your htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?shortID=$1 [L,QSA]
Then when you check the "shortID" you'd just reverse it to get the long ID:
$shortID = $_GET['shortID'];
$id = base_convert($shortID,32,10);
Related
$url = example.com/your-new-key-is.php?key=5&submit=success
The above url consists query string and submit value, also it's not user friendly
Once I click the submit the new url want to be like that
$url = example.com/your-new-key-is-5/
I want to replace the key url with value(5) and truncate the part followed & symbol.
Anybody knows the answer ?
I think you can:
$url1 = example.com/your-new-key-is.php?key=5&submit=success
$url2 = example.com/your-new-key-is-5/
<?php
$url= str_replace($url1 , $url2);
?>
You can see more examples here: function.str-replace.php
You need to use mod rewrite. There are ways to write them. You can also use a rewrite rule generator to understand how its done.
Below are the rules that needs to go into the .htaccess file
# Rewrite --- example.com/your-new-key-is.php?key=5&submit=success => example.com/your-new-key-is-5/
RewriteCond %{QUERY_STRING} (^|&)key=5($|&)
RewriteCond %{QUERY_STRING} (^|&)submit=success($|&)
RewriteRule ^example\.com/your-new-key-is\.php$ /example.com/your-new-key-is-5/?&%{QUERY_STRING}
Here is a link to the Generator.
http://www.visiospark.com/mod-rewrite-rule-generator/
After you've written the rule to htaccess, you will enter in example.com/your-new-key-is-5/ which is equivalent to example.com/your-new-key-is.php?key=5&submit=success
I have a similar rule in my .htaccess
RewriteRule ^news/([0-9]+)/([0-9]+)/([0-9]+)/(.*)/$ /?name=$4&year=$1&monthnum=$2&day=$3
Example
http://www.example.com/news/2014/02/25/stackoverflow-is-cool/
Now I am required to add in extra parameter at the back such that it looks like
http://www.example.com/news/2014/02/25/stackoverflow-is-cool/?source=email
I wish to get the source value and I tried the following:
RewriteRule ^news/([0-9]+)/([0-9]+)/([0-9]+)/([^/]*)/\?source=(.*)$ /?name=$4&year=$1&monthnum=$2&day=$3&source=$5
but it's not working. Any advice?
Thank you.
I haven't tried this myself, can you please give it a try
RewriteRule ^news/([0-9]+)/([0-9]+)/([0-9]+)/(.*)/$ /?name=$4&year=$1&monthnum=$2&day=$3 [QSA]
The [QSA] Query String Append flag will retain the existing query strings. So your query string
http://www.example.com/news/2014/02/25/stackoverflow-is-cool/?source=email
Should get converted to
/?source=email&name=$4&year=$1&monthnum=$2&day=$3
Please give it a try.
Try
RewriteRule ^news/([0-9]+)/([0-9]+)/([0-9]+)/([^/]*)/\?$ /?name=$4&year=$1&monthnum=$2&day=$3&source=$5
When you get source value..... just use
$SrcVal = str_replace('source=','',$_GET['source']);
We're trying to migrate our forum to another platform and we have encountered links which have queries in them, such as
http://forum.test/threads/119312-Warnight-CS-GO?p=2306618&viewfull=1#post2306618
which has to point to http://forum.test/threads/warnight-cs-go.119312/#post-2306618
So the logical structure of the original link is :
http://{forum_base_url}/threads/{thread-id}-{thread-permalink}?p={post-id}&viewfull={post-number-in-thread}#post{post-id}
While the new one is:
http://{forum_base_url}/threads/{thread-permalink}.{thread-id}/#post-{post-id}
So for the rewrite to work we need to "pull" three things out of the original link: the thread-id, the permalink, and the post-id. The first two aren't an issue, it's the third one which doesn't want to cooperate.
After scouring the Internet for possible solutions, we came up with:
RewriteCond %{QUERY_STRING} ^p=(\d+)&viewfull=(\d+)#post(\d+)$
RewriteRule ^threads/([0-9]+)-(.*)$ /threads/$2\.$1/#post-%1? [R=301,NC,L]
But unfortunately, the rewrite doesn't work.
What throws us off regarding the rewrite is that there are multiple variables in the query and no .php file specified in the link itself, so we can't just use the solution offered here: https://stackoverflow.com/a/2252242/1288397
Any tips on how to overcome this particular hurdle?
The #post- part of the URL is never sent to the server, it remains entirely on the browser's end, so there's no way to match against it. Luckily, the post ID is already in the query string so you can ignore it:
RewriteCond %{QUERY_STRING} ^p=(\d+)&viewfull=(\d+)$
RewriteRule ^threads/([0-9]+)-(.*)$ /threads/$2\.$1/#post-%1? [R=301,NC,L]
The other thing is that the thread-permalink is lowercase in your second URL. Not sure if that matters or not, but in order to change text to lowercase, you need to use a Rewrite Map, and the tolower internal function. You can only declare maps in the server vhost/config, so if you don't have access to those config files, you're not going to be able to use maps.
Thanks for clearing up the #post- aspect.
I tried your modification, but the links initially became:
http://forum.test/threads/warnight-cs-go.119312/%23post-2306618#post2306618
In order to work, the rewrite needs to be
RewriteCond %{QUERY_STRING} ^p=(\d+)&viewfull=(\d+)$
RewriteRule ^threads/([0-9]+)-(.*)$ /threads/$2\.$1/#post-%1? [R=301,NC,NE,L]
Notice the additional NE (noscape) flag at the end
That's because, by default, special characters, such as & and ?, will be converted to their hexcode equivalent. Using the [NE] flag prevents that from happening, as seen here: http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_ne
Using CodeIgniter route I have this rule
$route['^(?!login|profile|signup|notification|member).*/(:num)/(:any)/(:any)'] = "home/$2/$0/$1/$3";
My URL would be something like this http://example.com/memberfolder/7/view_document/WjbZpAPHM6
So basically the code should route to view_document() method in the home class with rsegment(5) being value WjbZpAPHM6
However, when the code goes to the home class, rsegment(5) value is view_document.
As you can see for my routing rule I should only have 5 rsegments, so when I printed all rsegments I got this:
home view_document memberfolder 7 view_document WjbZpAPHM6
As you can see my rsegment(5) is a duplicate or rsegment(2). Even though I should have only 5 rsegments, my document file value WjbZpAPHM6 is stored in rsegment(6).
Any advice on how to correct rsegment(5) to be the value of the document name rather then view_document
Also, my .htaccess is as such:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /example/index.php/$1 [L]
(:any) will match any character EXCEPT / in CI 3.0 or WILL match / in CI <= 2.1.3
So to make it compatible for both versions we will just manually put in the character match without using CI's shorthand syntax by using [^/]+ in place of :any. Note we do not put in parentheses as we do not want to create a backreference.
Also you do not put ^ as CI automatically puts it in. In addition, you have your backreferences wrong. You don't use $0, you start with $1 and work up. I changed them to correct numbers.
Your route should be this:
$route['((?!login|profile|signup|notification|member)|member[^/]+)/(:num)/(:any)/(:any)'] = "home/$3/$1/$2/$4";
So for the following URL example:
memberfolder/7/view_document/WjbZpAPHM6
The backreferences will be:
$1 = memberfolder
$2 = 7
$3 = view_document
$4 = WjbZpAPHM6
Now to build your request:
home/$3/$1/$2/$4 becomes home/view_document/memberfolder/7/WjbZpAPHM6
Making the following rsegments:
rsegment(1) = home
rsegment(2) = view_document
rsegment(3) = memberfolder
rsegment(4) = 7
rsegment(5) = WjbZpAPHM6
I don't know much about CodeIgniter, but I think, you should replace
$route['^(?!login|profile|signup|notification|member).*/(:num)/(:any)/(:any)'] = "home/$2/$0/$1/$3";
with this
$route['^(?!login|profile|signup|notification|member).*/(:num)/(:any)/(:any)'] = 'home/$2/$0/$1/$3';
I am trying to combine these four get variables (sch, val, lmt, ord) into a nice looking url re_write. Currently I have the following in my .htaccess file and it works if all variables are included, however, if I only input the first two variables (sch & val) it breaks:
RewriteRule ^search/([^/]*)/([^/]*)/([^/]*)/([^/]*)$
search.php?sch=$1&val=$2&lmt=$3&ord=$4 [L]
www.domain.com/search/City/London/5/asc works
www.domain.com/search/City/London but this doesn't
Why not just add the other rewrite rules ahead of it. IE:
RewriteRule ^search/([^/]*)$
search.php?sch=$1 [L]
RewriteRule ^search/([^/]*)/([^/]*)$
search.php?sch=$1&val=$2 [L]
RewriteRule ^search/([^/]*)/([^/]*)/([0-9]*)$
search.php?sch=$1&val=$2&lmt=$3 [L]
RewriteRule ^search/([^/]*)/([^/]*)/([0-9]*)/([^/]*)$
search.php?sch=$1&val=$2&lmt=$3&ord=$4 [L]
That should work. Not sure if it is what you want, but I do not really think it is "possible" to do that without just doing a query and passing the full query string as one line then parsing it PHP side.
EDIT
The nice thing about doing all this in .htaccess vs PHP is it requires less logic and you pass what you want to your script in the correct values. My preference would be using the .htaccess above.
you can use this , to grab whole path as text and parse
www.domain.com/search/City/London/5/asc works www.domain.com/search/City/London but this doesn't
RewriteRule ^search/(.*)$ search.php?url=$1
$paths=explode('/',$_GET('url'));
output
www.domain.com/search/City/London/5/asc
$paths=city,london,5,asc
www.domain.com/search/City/London
$paths=city,london
You can use loop and pair as key/value... if needed