2

my url formatmysite/profile/?theusernamewith hide php extension, im trying to hide question mark in url so the url will like mysite/profile/theusername, looked up few posts about what i should do is add external redirect then internal forward in htaccess, tried lot of code still can't get it work. this is what i have now:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /   

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
Cœur
  • 37,241
  • 25
  • 195
  • 267
XYZ_Allen
  • 253
  • 2
  • 3
  • 15
  • 1
    This may be answered here if you ignore `RewriteCond !{QUERY_STRING} id` http://stackoverflow.com/questions/4881410/apache-htaccess-how-to-remove-question-mark-from-url-if-not-id – Popnoodles Dec 07 '13 at 07:18
  • @popnoodles i think this one is for redirect link to upper dir like /foo/?bar to /foo – XYZ_Allen Dec 07 '13 at 07:41

1 Answers1

1

Keep your .htaccess like this:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /profile/

# new rules
RewriteCond %{THE_REQUEST} \s/+profile/?(?:index\.php)?\?([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ index.php?$1 [L,QSA]

# php hiding
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • this one works for hiding php extension, but my url still has the question mark like /profile/?theusername – XYZ_Allen Dec 07 '13 at 07:31
  • :( the url becomes: /profile////////////////////, this might help, the actual url: mysite/profile/index.php?theusername with htaccess hiding extension becomes /profile/?theusername – XYZ_Allen Dec 07 '13 at 07:50
  • Do you some other rule also in this .htaccess? – anubhava Dec 07 '13 at 07:51
  • FIrst you wrote your URI is: `mysite/profile/?theusername` and now you're saying URL is `mysite/profile/index.php?theusername` make sure to give correct info for getting best answer. – anubhava Dec 07 '13 at 07:55
  • sry about that my url is mysite/profile/?theusername with htaccess, im just telling u the file structure. nothing different. – XYZ_Allen Dec 07 '13 at 07:57
  • ok check updated answer now. Make sure you don't have any other rule. – anubhava Dec 07 '13 at 08:01
  • :( it bounce me back to /profile i have only above code in htaccess file. – XYZ_Allen Dec 07 '13 at 08:10
  • thx this one works well :) now i got new problem, it actually redirect me to profile/theusername....and its a 404, i still have to do a internal forward with external redirect? – XYZ_Allen Dec 07 '13 at 08:24
  • That internal rule is already there before PHP hiding which rewrites `/profile/theusername` to `/profile/index.php?theusername` – anubhava Dec 07 '13 at 08:28
  • oh then this one isn't working, in inspector i have /profile/?theusername (302) then /profile/theusername (301) then redirect to my 404 page – XYZ_Allen Dec 07 '13 at 08:38
  • Where is `/profile/theusername (301)` coming from? We don't have any 301 rule here. Do you have any other .htaccess somewhere? – anubhava Dec 07 '13 at 08:44
  • Also check edited answer one more time but that 301 is still a mystery. – anubhava Dec 07 '13 at 08:54
  • it was my root htaccess, i have custom 404 page there, thats why 301 shows. i removed it, now i got 404 on profile/theusername, i dont know why it doesn't redirect back to ?theusername – XYZ_Allen Dec 07 '13 at 08:58
  • `it was my root htaccess` So you have another .htaccess and this is not root? This is totally new. I asked you clearly to give all the info in question. – anubhava Dec 07 '13 at 09:01
  • ....rly my bad, i don't want to give too much useless info, i have only index document and custom 404 page in root htaccess, no any rewrite rule there, its empty. sry for the confuse. – XYZ_Allen Dec 07 '13 at 09:06
  • if u test and works on ur server then must be my server setting issue, ill take the result and thank you for keep answering my question. if its not about server setting i guess since /theusername is 404 then browser won't go any further (redirect).. – XYZ_Allen Dec 07 '13 at 09:16
  • You need to place all the relevant into your question. `.htaccess` rules vary depending on the directory it resides in. Above rules work fine for me in my root .htaccess. If you're placing it in some subfolder then let me know the name of the folder and I will make changed in the answer. – anubhava Dec 07 '13 at 09:21
  • I have edited the code above. Just make sure to replace `RewriteBase /sub/` with whatever your sub directory is. – anubhava Dec 07 '13 at 09:31
  • omg im so sryyyy i didn't know that it work so differently in different location. this is the file structure: root/profile/index.php for **index.php** its empty it only shows query_string, **root/** htaccess has hide php extension and index page codes. **profile/** htaccess is all ur rewrite code which works until /theusername(404) – XYZ_Allen Dec 07 '13 at 09:34