0

I was trying to shorten the url more, but I was removing .php or .html file extension from the urls using url rewriting too. I can't run both at the same time.

Removing the extensions and trying to shorten link from

site.com/profile.php?id={username}

to

site.com/username.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-z]+)$ profile.php?id=$1 [NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^\..*?$ $1.html [NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^\..*?$ $1.php [NC,L]

Just the first thing I'm trying to do works, or removing extension(but not both, not .html and .php at the same time), or removing profile.php?id, not both.

Community
  • 1
  • 1
Don Paul
  • 37
  • 8
  • have you working in any framework????? – Mohit Kumar Aug 16 '19 at 16:07
  • @MohitKumar no, just php & html for now. – Don Paul Aug 16 '19 at 16:08
  • @DonPaul Do yourself a favor and start using a framework, it does all of this for you. – GrumpyCrouton Aug 16 '19 at 16:08
  • @GrumpyCrouton I can't right now, I want to solve this problem. – Don Paul Aug 16 '19 at 16:09
  • @DonPaul That _would_ solve this problem – GrumpyCrouton Aug 16 '19 at 16:22
  • @GrumpyCrouton I can't move my entire project on a framework right now. – Don Paul Aug 16 '19 at 16:23
  • @Jonnix how can i modify it? Like (.)$ ? – Don Paul Aug 16 '19 at 16:24
  • @Jonnix still not working, the second rule. – Don Paul Aug 16 '19 at 16:26
  • @Jonnix I found out this https://webmasters.stackexchange.com/questions/34450/htaccess-two-different-rules-but-only-one-per-time but still not working. – Don Paul Aug 16 '19 at 16:40
  • Done. I didn't manage to solve it. – Don Paul Aug 16 '19 at 16:52
  • Still not working @Jonnix `RewriteRule \..*?$ $1.php [NC,L]` or `RewriteRule \..*?$ $1.html [NC] ` – Don Paul Aug 16 '19 at 17:01
  • Is the profile bit working? (Btw, for that rule you should add an `L` to the `[]` bit. – Jonnix Aug 16 '19 at 17:05
  • Okay, I got confused, this isn't going to work. By the sounds of it you want `http://example.com/username` to go to `profile.php?id=username`, but at the same time you want `http://example.com/mypage` to go to `mypage.html` or `mypage.php`. Is that right? (Have removed some previous comments to not confuse others.) – Jonnix Aug 16 '19 at 17:08
  • @Jonnix yes. This is right! – Don Paul Aug 16 '19 at 17:16
  • @DonPaul Then consider this, how would httpd know that `mypage` wasn't a username? The solution is to add an extra part to the URL e.g. have `/profile/username` rewrite to `profile.php?id=username` or similar. Something that makes it different from every other page. – Jonnix Aug 16 '19 at 17:17
  • Yes, this works. But I want a direct link, without profile. site.com/username, without /profile/ or /p/ or anything. – Don Paul Aug 16 '19 at 17:19
  • Okay, so another question. Are you fine with saying, if there is a file called `username.php` that /username would go to `username.php`, not to `profile.php`? – Jonnix Aug 16 '19 at 17:21
  • Yes, I have a constraint that the usernames contain only letters and numbers. – Don Paul Aug 16 '19 at 17:24
  • Can you read my comment again and think it through. You have a URL `/mypage` it has only letters or numbers (ignoring /), so is it a file to process, or is it a username? What is that decision based on? – Jonnix Aug 16 '19 at 17:27

1 Answers1

0

Wy has it to be with Rewrite? You can do

 function processAjaxData(response, urlPath){
 document.getElementById("content").innerHTML = response.html;
 document.title = response.pageTitle;
 window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath); }

like its in here: How do I modify the URL without reloading the page?

EDIT

I guess you can try this HERE

Alexis Garcia
  • 452
  • 3
  • 15
  • yes, but I want that link to be accessible from anywhere, not only from my website. – Don Paul Aug 16 '19 at 16:22
  • http://www.Something.com/?userID=2&profile....... normal url, you want to do this http://www.Something.com/id2 ? (just examples) without a framework like codeigniter, laravel or something else i dont know if its possible. I mean its possible but you will have a lot of work to do. Or you either change to a framework or you better change the url on page without reloading it – Alexis Garcia Aug 16 '19 at 16:30
  • Yes, I want to transform site.com/username and to remove .php extension like site.com/login.php to site.com/login . – Don Paul Aug 16 '19 at 16:34
  • No, I guess is not what I need. – Don Paul Aug 16 '19 at 16:43
  • you can do ````site.com/login.php to site.com/login```` with that link that i just gave you – Alexis Garcia Aug 16 '19 at 16:46