Question URL Rewrite

BayX

I'm the Root. Fear me!
Joined
Dec 18, 2015
Messages
74
Likes
26
Points
56
Location
Sweden
#1
Hey guys,
A little late but Happy New Year!

I'm working on a project that contains different function files and variables, which I want to make user/eye friendly. I'm not a very good friend with .htaccess so we have been fighting pretty much.. :p
There's more files but I will use 2 files for this example..

So in user.php I have the following links:
  • user.php (List users)
  • user.php?do=view&id=1 (View user with id 1)
  • user.php?do=view&id=1&action=test (A new function for actions)
And I also have a admin.php file with these links:
  • admin.php (View dashboard)
  • admin.php?do=users (List users)
  • admin.php?do=users&action=view&id=1 (View user with id 1)
  • admin.php?do=users&action=edit&id=1 (Edit user with id 1)
  • admin.php?do=settings (Settings for the website)
What I want is to make these links user/eye friendly, for example:
Code:
site.com/user.php -> site.com/user
site.com/user.php?do=view&id=1 -> site.com/user/view/1
site.com/user.php?do=view&id=1&action=test -> site.com/user/view/1/test
I have tried to search a lot for a solution, but whatever I try fails. Either it only works with 1 variable, or it only works with 2. I can't get all links to rewrite and use the amount of variables I want..
So now I would like your help folks.

I really tried to make it very clear what I'm looking to do, but just tell me if anything is confusing..
 
Last edited:

Cgamez

New Member
Joined
Apr 17, 2018
Messages
10
Likes
10
Points
8
Age
20
#2
Why do u want google to crawl ur *Secured* Admininterface? o_O It should be IP-restricted and with login btw...
 

BayX

I'm the Root. Fear me!
Joined
Dec 18, 2015
Messages
74
Likes
26
Points
56
Location
Sweden
#3
Why do u want google to crawl ur *Secured* Admininterface? o_O It should be IP-restricted and with login btw...
I'm sorry, I'm a little tired so didn't noticed I wrote that. I mean user/eye friendly, not seo friendly.
And btw, the admin section will be protected with both IP-restriction, password & OTP :)
 

kalle

high minded
Member of the Month
Contributor
Joined
Oct 28, 2015
Messages
312
Likes
157
Points
118
Age
23
Location
Bosnia and Herzegovina
#4
Hey guys,
A little late but Happy New Year!

I'm working on a project that contains different function files and variables, which I want to make user/eye friendly. I'm not a very good friend with .htaccess so we have been fighting pretty much.. :p
There's more files but I will use 2 files for this example..

So in user.php I have the following links:
  • user.php (List users)
  • user.php?do=view&id=1 (View user with id 1)
  • user.php?do=view&id=1&action=test (A new function for actions)
And I also have a admin.php file with these links:
  • admin.php (View dashboard)
  • admin.php?do=users (List users)
  • admin.php?do=users&action=view&id=1 (View user with id 1)
  • admin.php?do=users&action=edit&id=1 (Edit user with id 1)
  • admin.php?do=settings (Settings for the website)
What I want is to make these links user/eye friendly, for example:
Code:
site.com/user.php -> site.com/user
site.com/user.php?do=view&id=1 -> site.com/user/view/1
site.com/user.php?do=view&id=1&action=test -> site.com/user/view/1/test
I have tried to search a lot for a solution, but whatever I try fails. Either it only works with 1 variable, or it only works with 2. I can't get all links to rewrite and use the amount of variables I want..
So now I would like your help folks.

I really tried to make it very clear what I'm looking to do, but just tell me if anything is confusing..
Check MVC frameworks, they are great. Instead of making all of the job you can simply use framework to overwrite url. If you are begginer use CodeIgniter 3.0, for bigger projects Laravel or Symfony.
 

BayX

I'm the Root. Fear me!
Joined
Dec 18, 2015
Messages
74
Likes
26
Points
56
Location
Sweden
#5
Check MVC frameworks, they are great. Instead of making all of the job you can simply use framework to overwrite url. If you are begginer use CodeIgniter 3.0, for bigger projects Laravel or Symfony.
Right now I would rather not use a "complex" framework just to be able to rewrite the URLs. Maybe as/if the project grows, but not at the moment
 

kalle

high minded
Member of the Month
Contributor
Joined
Oct 28, 2015
Messages
312
Likes
157
Points
118
Age
23
Location
Bosnia and Herzegovina
#6
Right now I would rather not use a "complex" framework just to be able to rewrite the URLs. Maybe as/if the project grows, but not at the moment
CodeIgniter is easy, just implement MVC logic, and that will make ur web app more safe and secured and and url rewrite is there. :cool:
But if you are fan of plain PHP, go ahead. :oops:
 
Joined
Apr 11, 2016
Messages
22
Likes
1
Points
38
Location
Ukraine
#7
Hey guys,
A little late but Happy New Year!

I'm working on a project that contains different function files and variables, which I want to make user/eye friendly. I'm not a very good friend with .htaccess so we have been fighting pretty much.. :p
There's more files but I will use 2 files for this example..

So in user.php I have the following links:
  • user.php (List users)
  • user.php?do=view&id=1 (View user with id 1)
  • user.php?do=view&id=1&action=test (A new function for actions)
And I also have a admin.php file with these links:
  • admin.php (View dashboard)
  • admin.php?do=users (List users)
  • admin.php?do=users&action=view&id=1 (View user with id 1)
  • admin.php?do=users&action=edit&id=1 (Edit user with id 1)
  • admin.php?do=settings (Settings for the website)
What I want is to make these links user/eye friendly, for example:
Code:
site.com/user.php -> site.com/user
site.com/user.php?do=view&id=1 -> site.com/user/view/1
site.com/user.php?do=view&id=1&action=test -> site.com/user/view/1/test
I have tried to search a lot for a solution, but whatever I try fails. Either it only works with 1 variable, or it only works with 2. I can't get all links to rewrite and use the amount of variables I want..
So now I would like your help folks.

I really tried to make it very clear what I'm looking to do, but just tell me if anything is confusing..
Do you still need to know this? I decided for the occasion to write my little helper.
 

BayX

I'm the Root. Fear me!
Joined
Dec 18, 2015
Messages
74
Likes
26
Points
56
Location
Sweden
#8
Do you still need to know this? I decided for the occasion to write my little helper.
That would be great, yes. I will wait a bit with a framework, so I'm still open for any kind of help
 
Joined
Mar 20, 2016
Messages
20
Likes
0
Points
38
Age
28
#9
Hey guys,
A little late but Happy New Year!

I'm working on a project that contains different function files and variables, which I want to make user/eye friendly. I'm not a very good friend with .htaccess so we have been fighting pretty much.. :p
There's more files but I will use 2 files for this example..

So in user.php I have the following links:
  • user.php (List users)
  • user.php?do=view&id=1 (View user with id 1)
  • user.php?do=view&id=1&action=test (A new function for actions)
And I also have a admin.php file with these links:
  • admin.php (View dashboard)
  • admin.php?do=users (List users)
  • admin.php?do=users&action=view&id=1 (View user with id 1)
  • admin.php?do=users&action=edit&id=1 (Edit user with id 1)
  • admin.php?do=settings (Settings for the website)
What I want is to make these links user/eye friendly, for example:
Code:
site.com/user.php -> site.com/user
site.com/user.php?do=view&id=1 -> site.com/user/view/1
site.com/user.php?do=view&id=1&action=test -> site.com/user/view/1/test
I have tried to search a lot for a solution, but whatever I try fails. Either it only works with 1 variable, or it only works with 2. I can't get all links to rewrite and use the amount of variables I want..
So now I would like your help folks.

I really tried to make it very clear what I'm looking to do, but just tell me if anything is confusing..
Google it ^^

Where u are
You do not have permission to view link Log in or register now.
 

BayX

I'm the Root. Fear me!
Joined
Dec 18, 2015
Messages
74
Likes
26
Points
56
Location
Sweden
#10
Google it ^^

Where u are
You do not have permission to view link Log in or register now.
Since I'm not best friend with .htaccess, I have been Googleing a lot, and I refer to what I said in my first post:
Either it only works with 1 variable, or it only works with 2. I can't get all links to rewrite and use the amount of variables I want..

So after messing around with the .htaccess file, I always end up on square one again...
 
Joined
Mar 20, 2016
Messages
20
Likes
0
Points
38
Age
28
#11
Since I'm not best friend with .htaccess, I have been Googleing a lot, and I refer to what I said in my first post:
Either it only works with 1 variable, or it only works with 2. I can't get all links to rewrite and use the amount of variables I want..

So after messing around with the .htaccess file, I always end up on square one again...
Broowww!

Code:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /user.php?do=view&id=$1 [L]  # -> site.com/user/view/1 
RewriteRule ^([^/]*)/([^/]*)$ /user.php?do=view&id=$1&action=$2 [L]  #-> site.com/user/view/1/test
try like this
 

BayX

I'm the Root. Fear me!
Joined
Dec 18, 2015
Messages
74
Likes
26
Points
56
Location
Sweden
#12
I hope you didn't take it as something negative. I just pointed out that I already said it before :p

Your example don't work. I get 404 error. This kind of works:
Code:
# Remove .php extension
RewriteRule ^([^/.]+)$ $1.php [L]

# Make crappy URLs look pretty
RewriteRule ^user/([^/]*)/([^/]*)$ user.php?do=$1&id=$2 [L]
RewriteRule ^user/([^/]*)/([^/]*)$ user.php?do=$1&id=$2&action=$2 [L]
Notice user/ in the beginning and $1 after ?do=..
The part that doesn't work is that if I go to /user/view/1 no style is showing, just a black/white page. And if I go to /user/view/1/test I get a 404 error.
That's what I meant with "I always end up on square one again" :rolleyes:
 
Joined
Mar 20, 2016
Messages
20
Likes
0
Points
38
Age
28
#13
I hope you didn't take it as something negative. I just pointed out that I already said it before :p

Your example don't work. I get 404 error. This kind of works:
Code:
# Remove .php extension
RewriteRule ^([^/.]+)$ $1.php [L]

# Make crappy URLs look pretty
RewriteRule ^user/([^/]*)/([^/]*)$ user.php?do=$1&id=$2 [L]
RewriteRule ^user/([^/]*)/([^/]*)$ user.php?do=$1&id=$2&action=$2 [L]
Notice user/ in the beginning and $1 after ?do=..
The part that doesn't work is that if I go to /user/view/1 no style is showing, just a black/white page. And if I go to /user/view/1/test I get a 404 error.
That's what I meant with "I always end up on square one again" :rolleyes:
You in your file u have <base> ? If no try to put to see if css start to load...
<base href = "
You do not have permission to view link Log in or register now.
" />
^^ im trying to help you i dont get neggative
 

BayX

I'm the Root. Fear me!
Joined
Dec 18, 2015
Messages
74
Likes
26
Points
56
Location
Sweden
#14
You in your file u have <base> ? If no try to put to see if css start to load...
<base href = "
You do not have permission to view link Log in or register now.
" />
^^ im trying to help you i dont get neggative
When I used base, everything went crazy. I tried that yesterday and it didn't work at all.

Now I know why there wasn't any styling when browsing /user/view/1. First of all, the project is inside a folder, so index.php tried to pull vendor/bootstrap/css/bootstrap.min.css from folder/user/view/1/ instead of just folder. So I made a function to grab protocol and HTTP_HOST, and after that everything works :D
 

Asphyxia

Server Monkey
Administrator
Joined
Apr 25, 2015
Messages
946
Likes
1,412
Points
217
Age
25
Location
North America
#16
@BayX sometimes the hard part is finding the right search to enter into Google. Are you able to take a look at any of these examples to tell me if you find something that makes more sense?

You do not have permission to view link Log in or register now.

You do not have permission to view link Log in or register now.


If neither of these help you get exactly what you want accomplished - let me know and I will make a video tutorial. If you see parts you are unsure of or need any special help on specific pieces - feel free to ask any questions.
 

BayX

I'm the Root. Fear me!
Joined
Dec 18, 2015
Messages
74
Likes
26
Points
56
Location
Sweden
#17
@BayX sometimes the hard part is finding the right search to enter into Google. Are you able to take a look at any of these examples to tell me if you find something that makes more sense?

You do not have permission to view link Log in or register now.

You do not have permission to view link Log in or register now.


If neither of these help you get exactly what you want accomplished - let me know and I will make a video tutorial. If you see parts you are unsure of or need any special help on specific pieces - feel free to ask any questions.
I searched for a while and did actually end up to look at just those links you submitted. I'll go with the solution I found for now, but I will contact you if I need any help if/when I want to change the structure of the project :)
 

Top