Jump to content

Recommended Posts

Hi

 

I am using this code in .htaccess but cant find a correct solution that would automatically put slash at the end or url if missing. Can you please help me with it...

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^([A-Za-z0-9-]+)$ profile/$1/ [R]
RewriteRule (.*)/$ view.php?profile=$1

 

I cant work out why does not this code work properly - ending the url with slash

 

Link to comment
https://forums.phpfreaks.com/topic/184644-slash-at-the-end-of-url/
Share on other sites

Well the exact rewrite would depend on your actual data structure. An example...

 

RewriteRule ^([A-Za-z0-9-]+)$ /profile/$1/ [R]
RewriteRule ^profile/(.*)/$ /view.php?profile=$1

 

Will change the users address bar from http://domain.com/bob to http://domain.com/profile/bob/, the user will actually see http://domain.com/view.php?profile=$1. If that's not what your after (which I don't think it is) you will have to be more specific giving an example url you wish to start with, what it should be changed to and what they should actually see.

two things to note here:

 

1. this code will append a forward slash to the end of a URL if one is not present:

RewriteCond %{HTTP_HOST} !^www.your-site.com$ [NC]
RewriteRule ^(.*)$ http://www.your-site.com/$1 [R=301,L]

 

2. this one gives allows for either by having a ? immediately following the /:

RewriteRule ^([A-Za-z0-9-]+)/?$ view.php?profile=$1 [NC,L]

That URL doesn't match your RewriteRule. Your pattern matches something that only contains a-z, 0-9 or a dash. I'd guess that you've done nothing to set your site up in Apache thus making your DocumentRoot http://127.0.0.1/ meaning that the pattern you need to match is mywebsite/profile/name which contains forward slashes. Your pattern to add a trailing slash would probably need to be something more like...

 

RewriteRule ^(mywebsite/profile/[A-Za-z0-9-]+)$ $1/ [R]

That URL doesn't match your RewriteRule. Your pattern matches something that only contains a-z, 0-9 or a dash. I'd guess that you've done nothing to set your site up in Apache thus making your DocumentRoot http://127.0.0.1/ meaning that the pattern you need to match is mywebsite/profile/name which contains forward slashes. Your pattern to add a trailing slash would probably need to be something more like...

 

RewriteRule ^(mywebsite/profile/[A-Za-z0-9-]+)$ $1/ [R]

 

I am sorry I tried according to this and it still does not work

Missed a slash...

 

RewriteRule ^(mywebsite/profile/[A-Za-z0-9-]+)$ /$1/ [R]

 

...should work, I tested it on my end.

 

What is wrong with my server, It didnt work again... I tried several different changes but still no success. Is the second line all right together with the first one?

 

So just to confirm, you are saying that if you have a .htaccess file with just the following in it...

 

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^(mywebsite/profile/[A-Za-z0-9-]+)$ /$1/ [R]

 

The slash is not added in the address bar.

Not that I am aware of... The only time when the rewrite rule added the slash was when I was using instead of name a number

 

http://127.0.0.1/mywebsite/profile/name/
http://127.0.0.1/mywebsite/profile/0123456/

 

With url ending with a number it added the slash however it never worked for me with url ending with string

 

 

Oh... good point, but still if I remove the fullstop from the url the browser says page not found (not the server this time the browser)

 

I dont get it really, the mod rewrite definition does not specify fullstop character but as long as its ended with slash its fine - how come?

Page not found is exactly the error you want, providing it has added the slash, which is all we we're working on. Once that is working correctly we can work on the second redirect to serve up a different page to what the address says. Your second rule will want to be something along the lines of...

 

RewriteRule ^mywebsite/profile/([A-Za-z0-9-]+)/$ /mywebsite/profile/view.php?profile=$1/ 

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.