Jump to content

Recommended Posts

Hello,

I'm working on script, and want to set links to be as following:

www.mysite.com/sign-up.php
to
www.mysite.com/sign-up

I found this code and works for me.

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

BUT if I add a slash at the end of link, ex: www.mysite.com/sign-up/

it shows me this error

 

 

 

  1. Internal Server Error
  2.  
  3. The server encountered an internal error or misconfiguration and was unable to complete your request.
  4.  
  5. Please contact the server administrator at admin@example.com to inform them of the time this error occurred, and the actions you performed just before this error.
  6.  
  7. More information about this error may be available in the server error log.
     

 

Q2- if the user try to access any php extension, I want him to redirect to same link without extension

EX: www.mysite.com/sign-up.php
to
www.mysite.com/sign-up
Thank you
 

 

Link to comment
https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/
Share on other sites

I take it the extensionless links didn't work before you put that rewriting in?

 

Did you see how it said

More information about this error may be available in the server error log.

So... have you looked in the server error log for more information about the error?

I take it the extensionless links didn't work before you put that rewriting in?

 

Did you see how it said

So... have you looked in the server error log for more information about the error?

 

here is the error log

[Mon Aug 05 13:50:32.523228 2013] [authz_core:error] [pid 6024:tid 1636] [client ::1:62516] AH01630: client denied by server configuration: C:/wamp/www/adsgate/.htaccess, referer: http://localhost/
[Mon Aug 05 13:50:40.452223 2013] [core:error] [pid 6024:tid 1636] [client ::1:62516] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Mon Aug 05 13:50:42.275454 2013] [core:error] [pid 6024:tid 1636] [client ::1:62523] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Mon Aug 05 13:51:51.726193 2013] [core:error] [pid 6024:tid 1632] [client ::1:62670] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

 

You should add the slash at the end as an option in the URL. 

 

Change:

RewriteRule ^(.+)$ $1.php [L,QSA]

to

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

thanks for your reply, I tried that but still shows me error

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at admin@example.com to inform them of the time this error occurred, and the actions you performed just before this error.

More information about this error may be available in the server error log.

This regExp accept only letters, numbers and underscores inside file's name.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([a-zA-Z_0-9]+)/?$ $1\.php [L,QSA]

PS: The slash symbol is optional.

Edited by jazzman1

Because, the "/" symbol has a special meaning for apache server.

Just apply a new rule and remove it from the url if it exists. 

 

Try,

RewriteEngine On
RewriteBase /work_dir
RewriteRule (.*)/$ $1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([a-zA-Z0-9_]+)$ $1\.php [L,QSA]

Note: Replace /work_dir with your current working directory!

Edited by jazzman1

thanks,

 

there is some small prblem, if my file name contain "-" then wil show the file not found, otherwise works fine.

 

note: I would like to use the slash, I need it for user's profile

 

ex: www.mydomain.com/profile/username/

Edited by KhaledBawz

So, the solution above could cause some problems.

 

For example if the php file contains the same name as a sub-directory, let's say - /bash.php and /bash/ (it's a directory).

 

To avoid that, make sure that the name of the directory has not the same name as the file that you want to rewrite.

 

So, change that:

RewriteEngine On
RewriteBase /work_dir
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule (.*)/$ $1 [R,L]
RewriteRule ^([a-zA-Z0-9_]+)$ $1\.php [L,QSA]

If the file itself contains a "-" character(s), just add it to the regExp.

RewriteRule ^([a-zA-Z0-9_-]+)$ $1\.php [L,QSA]
Edited by jazzman1

thanks jazzman1 everything is works fine. 

 

ex: www.mydomain.com/profile/username/

 

the above link is working but again that page will not include css :(

 

all links in above link will be like

 

I add this code

RewriteRule ^profile/(.*)$ profile.php?username=$1 [L] 

www.mydomain.com/profile/sign-up

 

and it should be

 

www.mydomain.com/sign-up

 

I'm sorry I asked you so many questions 

Edited by KhaledBawz

Solution:

 

Inside your view file, in that example profile.php, create a pretty link.

 

Something like:

<a href=./$userName> Username</a>

And apply a third rule inside .htaccess file. 

RewriteRule ^/?(.*?)$ profile.php?username=$1 [L]

I'm not that expert in php, but want to show you my idea

 

I would like to get the username form the link itself, if there a username in database has the same name in link then I'll print all his information, otherwise I'll show there no a user with that name.

 

all the codes to do that should be in profile.php

 

the code you put it in your last comment doesn't work. it keep shows profle.php at all time, if I click on sign-up.php it also print profile.php

 

this code is working except css style

RewriteRule ^profile/(.*)$ profile.php?username=$1 [L] 

Sorry for the delay, but I should have to pay the bills :)

 

So, inside profile.php create this ( just for test)

 

profile.php

<link rel="stylesheet" href="stylesheets/style.css" type="text/css">

<?php if(!isset($_GET['username'])) {echo '<a href=./profile/jazzman>UserName</a>';} else {
echo '<pre>'.print_r($_GET, true).'</pre>';    
}

The user name should be jazzman and will come dynamically  from DB

 

.htaccess file

RewriteEngine On
RewriteBase /work_dir
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule (.*)/$ $1 [R,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ $1\.php [L,QSA]
RewriteRule ^/?profile/([a-zA-Z_]+)$ profile.php?username=$1 [L,R=302]

Run that script and tell me what result you get.

Edited by jazzman1

Hey KhaledBawz,

so, the best way in that case is to create a new directory inside "/" web root.

For example, for all css stylesheets files you would create a directory with name stylesheets. 

And know the path to this directory is gonna be /stylesheets/style.css not ./stylesheets/style.css

That's all.

 

Try that now:

 

profile.php

<link rel="stylesheet" href="/stylesheets/style.css" type="text/css">

<?php if(!isset($_GET['username'])) {echo '<a href=./profile/jazzman>UserName</a>';} else {
echo '<pre>'.print_r($_GET, true).'</pre>';    
}

.htaccess

RewriteEngine On
RewriteBase /work_dir
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule (.*)/$ $1 [R,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ $1\.php [L,QSA]
RewriteRule ^/?profile/([a-zA-Z_]+)$ profile.php?username=$1 [L]

If you have images doing on the same way and logic.

Edited by jazzman1

I appreciate your help but I have the same problem, I gave up with there links. I'll use the default links :(

 

thank you Jazzman1

 

No, it's imposibble. You are doing something wrong. The path to the stylesheets directory is absolutely and independent from your project directory. Do you know what a web root folder is?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.