Jump to content

Mod rewrite only working sometimes

Ssoele

I have found a strange problem when I was working on a new project.
 
When making some clean URL's, I found that some just didn't want to work, in the code below, line 4 and 5 would work, but line 6, 7 and 9 would not.

RewriteEngine OnRewriteBase /RewriteRule ^home$ index.php [L]RewriteRule ^stats$ stats.php [L]RewriteRule ^signup$ signup.php [L]RewriteRule ^signin$ signin.php [L]RewriteRule ^dashboard$ dashboard.php [L]

When I just add a - to the URLs, line 6 and 7 do work.

RewriteEngine OnRewriteBase /RewriteRule ^home$ index.php [L]RewriteRule ^stats$ stats.php [L]RewriteRule ^sign-up$ signup.php [L]RewriteRule ^sign-in$ signin.php [L]RewriteRule ^dashboard$ dashboard.php [L]

Does anyone have any idea on what might be causing this? I don't have the slightest clue.

Link to comment
Share on other sites

Link to post
Share on other sites

The - (Dash) is a syntax used in regular expressions to define a range.

So for example if you want to grab the alphabet then you can do a-z and it would grab the range between and including a and z.

 

You'd need to escape the dashes using the \ (backward slash).

 

So anywhere you want to use a regex syntax as a character just use the \ to escape them but otherwise at times there are better ways to escape characters but here you're fine just using the \.

 

So you'd have

RewriteRule ^sign\-in$ signin.php [L]
Link to comment
Share on other sites

Link to post
Share on other sites

 

The - (Dash) is a syntax used in regular expressions to define a range.

So for example if you want to grab the alphabet then you can do a-z and it would grab the range between and including a and z.

 

You'd need to escape the dashes using the \ (backward slash).

 

So anywhere you want to use a regex syntax as a character just use the \ to escape them but otherwise at times there are better ways to escape characters but here you're fine just using the \.

 

So you'd have

RewriteRule ^sign\-in$ signin.php [L]

 

The problem is that it works with the dash and does not work without it.

Link to comment
Share on other sites

Link to post
Share on other sites

The problem is that it works with the dash and does not work without it.

that's a strange one, I'll give it a try when I'm home.

Link to comment
Share on other sites

Link to post
Share on other sites

that's a strange one, I'll give it a try when I'm home.

 

I found the problem, it's apparently because signin.php, signup.php and dashboard.php already exist as a file (I hadn't made stats.php yet). If I delete those files, it works.

 

I also did it a bit different now, everything is redirected to index.php and that one is getting the right page that is located in a different folder.

RewriteEngine OnRewriteBase /RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
Link to comment
Share on other sites

Link to post
Share on other sites

 

I found the problem, it's apparently because signin.php, signup.php and dashboard.php already exist as a file (I hadn't made stats.php yet). If I delete those files, it works.

 

I also did it a bit different now, everything is redirected to index.php and that one is getting the right page that is located in a different folder.

RewriteEngine OnRewriteBase /RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ index.php?page=$1 [L,QSA]

 

Laravel does it that way aswell. It's just a preconfigured htaccess file which routes to a server file and there it manages the links.

So they'd have server.php which does all the routing seperate from the processing, it would just return links to pages.

Link to comment
Share on other sites

Link to post
Share on other sites

 

I found the problem, it's apparently because signin.php, signup.php and dashboard.php already exist as a file (I hadn't made stats.php yet). If I delete those files, it works.

 

I also did it a bit different now, everything is redirected to index.php and that one is getting the right page that is located in a different folder.

RewriteEngine OnRewriteBase /RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ index.php?page=$1 [L,QSA]

Please don't do this directly with filenames. It opens up a whole new world of security-related issues.

Link to comment
Share on other sites

Link to post
Share on other sites

Please don't do this directly with filenames. It opens up a whole new world of security-related issues.

 

In PHP, I have enough checks to prevent abuse.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×