Hack Forums
[PHP] htaccess rewrite? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] htaccess rewrite? (/showthread.php?tid=5535225)



htaccess rewrite? - Skidy - Jan 27, 2017

Dont know where to put this other than here. If its in the wrong section please just tell me.

So I am trying to rewrite www.mysite.com/view.php?code=ANY_LETTER_OR_NUMBER

to www.mysite.com/view/ANY_LETTER_OR_NUMBER

This rewrties my .php to just /view/

PHP Code

<?php 
RewriteCond 
%{SCRIPT_FILENAME} !-d
RewriteCond 
%{SCRIPT_FILENAME} !-f
RewriteRule 
^(.*)$ $1.php 

How can I do this?


This is what I have currently:

RewriteRule ^view/(.+)$ /view.php?code=$1 [L,QSA]

Doesnt work


* - Specs - Jan 27, 2017

http://stackoverflow.com/questions/12138697/htaccess-rewrite-php-to-virtual-folder

This should help you somewhat, i never made it look like a directory before. For a shortener i worked on i used this:
RewriteRule ^(\w+)$ filename.php?id=$1

It just left with the id/num but without the directory.


* - Skidy - Jan 28, 2017

(Jan 27, 2017 10:45 PM)Yani Wrote:  Is this your whole file? Maybe you have something above that rule that fires first?

Yeah its my whole file. Want it to take a directory under view.


* - Skidy - Jan 28, 2017

(Jan 28, 2017 04:14 AM)Yani Wrote:  You have to add this above the ruleset:

Code
RewriteEngine On

I have that on already. Still doesn't work.
Any ideas??


* - Skidy - Jan 28, 2017

(Jan 28, 2017 04:45 AM)Yani Wrote:  Maybe look at your apache access log?

Should this work then?

Code
RewriteRule ^view/(.+)$ /view.php?code=$1 [L,QSA]



* - NoSuchAgency - Jan 29, 2017

Try this

Code
RewriteEngine On
RewriteRule ^view/([^/]*)\.php$ /view.php?code=$1 [L]

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]



* - Skidy - Jan 29, 2017

(Jan 29, 2017 04:49 PM)Poliwag™ Wrote:  Try this

Code
RewriteEngine On
RewriteRule ^view/([^/]*)\.php$ /view.php?code=$1 [L]

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

It works but all of my code is gone weird.

https://hisky.space/view/car

Look at that URL. It makes the page lose some of its HTML.


It seems to break here:

https://gyazo.com/103134b68a235d29acc0b14fbebd5265


* - NoSuchAgency - Jan 29, 2017

Hmm weird. It shouldn't be doing that. I am at work but I'll be home again in 4 hours to give a better look. If you send me the source code (or rather access to the server) I'll see what's up


* - Skidy - Jan 29, 2017

(Jan 29, 2017 06:03 PM)Poliwag™ Wrote:  Hmm weird. It shouldn't be doing that. I am at work but I'll be home again in 4 hours to give a better look. If you send me the source code (or rather access to the server) I'll see what's up

Brilliant, thank you. Ill send it now


* - NoSuchAgency - Jan 29, 2017

Try turning the [L] to [L,QSA]


* - Skidy - Jan 29, 2017

(Jan 29, 2017 06:05 PM)Poliwag™ Wrote:  Try turning the [L] to [L,QSA]

https://gyazo.com/920a32cc409197471998ae013b037dc1

Get a 500 error now.


(Jan 29, 2017 06:05 PM)Poliwag™ Wrote:  Try turning the [L] to [L,QSA]

Thank you btw, I send my code over.