URL Rewrite

BayX

I'm the Root. Fear me!
Dec 18, 2015
75
27
96
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
Apr 17, 2018
10
10
18
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!
Dec 18, 2015
75
27
96
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
Contributor
Oct 28, 2015
411
253
178
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!
Dec 18, 2015
75
27
96
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
Contributor
Oct 28, 2015
411
253
178
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:
 

k1t00

Member
Apr 11, 2016
31
2
43
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!
Dec 18, 2015
75
27
96
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
 

dumpmyself

Member
Mar 20, 2016
15
0
38
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
https://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049
 

BayX

I'm the Root. Fear me!
Dec 18, 2015
75
27
96

dumpmyself

Member
Mar 20, 2016
15
0
38
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!
Dec 18, 2015
75
27
96
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:
 

dumpmyself

Member
Mar 20, 2016
15
0
38
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 = "https://example.com" />
^^ im trying to help you i dont get neggative
 

BayX

I'm the Root. Fear me!
Dec 18, 2015
75
27
96
You in your file u have <base> ? If no try to put to see if css start to load...
<base href = "https://example.com" />
^^ 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

Owner
Administrator
Apr 25, 2015
1,844
2
2,197
327
@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?

https://stackoverflow.com/a/12123590
https://serverfault.com/q/210734

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!
Dec 18, 2015
75
27
96
@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?

https://stackoverflow.com/a/12123590
https://serverfault.com/q/210734

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