Change Of Complex To Simple URL & Remove Index.php - php

I have been trying & searching alot but couldn't find any solution for i have tried many things in .htaccess
My website's Url
mysite.org/torrents.php
To this
mysite.org/Browse
This
mysite.org/torrents.php?parent_cat=Movie
to this
mysite.org/Movie
This
mysite.org/account-details.php?id=737
to this
mysite.org/user/username

Your links won't all work properly, i.e. the /user/username one won't work to an ID number, you could pass the username through then do a lookup based on the username, but htaccess won't know that username "tim" is id number "123"
This should work:
RewriteEngine On
RewriteRule "user/([0-9]*)" account-details.php?id=$1
RewriteRule "category/([a-zA-Z0-9_-]*)" torrents.php?parent_cat=$1
RewriteRule "browse" torrents.php
RewriteRule "" torrents.php
With that I've separated out the categories into a subfolder called 'category' this makes it far easier to allow any category name you want.
For the regular expression matches in the RewriteRules theres an excellent free program called 'Expresso' that will help you build up regular expressions and test them against a list of values to see if they match, I'd recommend searching for it and getting a copy as it's very handy.
Also for the username part if you wanted to do username instead of user ID numbers, then swap:
RewriteRule "user/([0-9]*)" account-details.php?id=$1
to this:
RewriteRule "user/([a-zA-Z0-9_-]*)" account-details.php?username=$1

Try using this. it is a htaccess generator. If this doesn't work you will need to check you php.ini settings to see if it allows this type of link.

Related

ID from a URL that does not include ?=id

I'm currently making a website (with PHP and a database) and there something I'm wondering about.
Can you get an ID from a URL like this for example egh95.com/u1 or egh95.com/t52-it-s-a-new-day?
Like I only want the int to be my ID. I tried to use $_Server["REQUEST_URI"] and strip all the characters and symbols from the URL, but the int I got, didn't work to get the information out of the database.
I know how to use $_GET["id"] to get the int from the URL (that's the only thing that works atm), but I actually don't want the URL to be ?=id1 or something like that.
I'd just use a basic regex, if that's the case.
$url = 'egh95.com/u1';
preg_match('/^.*\/u(?P<data>.*)/', $string, $matches);
echo $matches['data'];
$matches['data'] contains the number after u, in this case 1. Here are some docs for regex in PHP, if you're interested.
If you use Apache, you can use ModRewrite to implement user-friendly URLs. It is easy way to achieve what you wrote about with small amount of work. Then write in .htaccess file following lines: (Assuming you want to process the id in index.php file)
Options FollowSymLinks
RewriteEngine On
RewriteRule "^u(.*)$" "index.php?id=$1"

.htaccess slash in name get variable issue

Hi and sorry for bothering you, i try to look around but i couldn't find a solution, and i really can't figure out how and if it's possible to do something like that with rewriterule.
This is the portion of htaccess file interested in my problem:
RewriteRule ^pagename/([^/]+)-([^/]+) mypage.php?name=$1&id=$2 [NC,L]
example.com/mypage/dogarticle-800ad43
so my variable in php will be like
$_GET["name"]=dogarticle and $_GET["name"]=800ad43
so far so good, but if an article contain a slash i can't get the right variable anymore, example
example.com/mypage/animals/dogandcat-800ad44
where animals/dogandcat should be name i receive the following get vars:
$_GET["name"]=animals $_GET["name"]=dogandcat
Here the question what should i do to receive the following get var:
$_GET["name"]=animals/dogandcat $_GET["name"]=800ad44>
Cant really figure out the solution, probably not the best of my days.
Thanks
edit:
in the name there can be a lot of slash example:
example.com/mypage/animals/dogandcat/squirrel/someotheranimal-800ad44
You can use:
RewriteRule ^pagename/(.+)-([^/]+)$ mypage.php?name=$1&id=$2 [NC,L]

htaccess rewrite rule with query string, multiple variables and no php file in the link

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 two GET variables in .htaccess (having problems outputting)

I'm currently trying to add a second get variable to an existing htaccess rule. I have little experience with the htaccess but using what expressions I know, I think it's missing one element to ensure the second variable is outputted correctly.
The domain (as the user sees it);
http://domain.com/london-area-info/?src=go
The rule i'm trying to apply;
RewriteRule ^(.*)-area-info/(.*)$ regioncc.php?region=$1&src=$2
The resulting output URL;
http://domain.com/regioncc.php?region=london&src=
I appreciate I'm probably overlooking something but looking around I haven't found an explanation has to how to make sure the ?src=go is shown after the rewrite rule rather than in another directory like /xxx-area-info/src/ for example.
EDIT
I have also tried the following rule but wasn't sure if I was escaping correctly
RewriteRule ^(.*)-area-info/(\?src=?.*)?$ regioncc.php?region=$1&src=$2
I believe this is what you want:
RewriteRule ^(.*)-area-info regioncc.php?region=$1&%{QUERY_STRING}

URL routing/rewriting and compatibility with Nginx/Lighttpd

I'm writing a simple URL routing code based on using mod_rewrite to pass the URI as a GET parameter, like Drupal does. So I have the rule:
RewriteRule ^(.*)$ index.php?q=$1 [QSA,L]
And the URL http://www.example.com/test/1 would give me "/test/1/" passed as value $_GET['q'], instead of the usual index.php/test/1 and having to extract that from $_SERVER['REQUEST_URI'].
The thing is, the mod_rewrite QSA flag allows me to still use query strings normally, which I find very useful for parameters like filters and pagination, like "/products/category/?pg=1".
Will this work the same on Nginx and Lighttpd servers? I'd like my code to be portable.
Thanks.
No, in either case you'll have to translate your rules into each server's specific syntax. Some links to get you started:
http://forums.digitalpoint.com/showthread.php?t=187965
http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModRewrite
https://serverfault.com/questions/24243/nginx-support-for-htaccess-rewrite-rules-differences-from-apache

Categories