Forum Moderators: phranque

Message Too Old, No Replies

Add trailing slash to the end of url and redirect to index.php

Url rewriting

         

sandeeppatil

11:37 am on Jul 2, 2008 (gmt 0)

10+ Year Member



Hi
My project is <snip>
Here i use URL rewriting in .htaccess file.I redirected every URL like
http://example.com/search.php/ to the index.php page.

I used .htaccess file and write below code

RewriteEngine on
RewriteRule ^(.+)/$ index.php?$1 [L]

My problem is that,when somebody type http://example.com/search.php
here trailing slash("/")not added so this URL not redirect to the index.php page.

Please give me solution ,My project is going to release shortly

[edited by: engine at 11:45 am (utc) on July 2, 2008]
[edit reason] examplified [/edit]

Marcia

11:46 am on Jul 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My problem is that,when somebody type http://example.com/search.php
here trailing slash("/")not added so this URL not redirect to the index.php page.

First off, the trailing slash is for a /folder/ and search.php is a page so that needs to be defined.

Secondly, you really don't want to redirect to example.com/index.php - you should redirect to example.com/ instead, but not all different pages should be redirected to the homepage to avoid duplication errors and confusion.

Third, check the forum library for relevant threads, and especially see the one on writing regular expressions and on how to do a 301 redirect.

Added:
In addition, are there query strings after search.php - like search.php?id=widgets

[edited by: Marcia at 11:50 am (utc) on July 2, 2008]

sandeeppatil

11:57 am on Jul 2, 2008 (gmt 0)

10+ Year Member



Not understood

Marcia

12:28 pm on Jul 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here i use URL rewriting in .htaccess file.I redirected every URL like
http://example.com/search.php/ to the index.php page.

Using example.com, not your real URL:

If every URL is like

http://example.com/search.php/

What do some of the other URLs look like, that are like that?

http://example.com/search.php/1
http://example.com/search.php/2

What comes after search.php on other pages?

sandeeppatil

12:42 pm on Jul 2, 2008 (gmt 0)

10+ Year Member



I mean to say
If my URL is http://example.com/search.php/ ,I forward User to the index.php page and on that page i extract "search" from URL "http://example.com/search.php/" and include search.php page there means i consider action is "search" here.

If next URL is http://example.com/login.php/ ,I forward User to the index.php page and on that page i extract "login" from URL "http://example.com/search.php/" and include login.php page there means i consider action is "login" here.

I have done this because i want every URL should redirect to the index.php so i pick the action and include corresponding page.

In addition, There are no query strings after search.php - like search.php?id=widgets

jdMorgan

1:22 pm on Jul 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To directly answer your question, you could change your code:

RewriteEngine on
RewriteCond $1 !^index\.php$
RewriteRule ^(.+)[b]/?$[/b] index.php?$1 [L]

The new RewriteCond is now required to prevent an 'infinite' rewriting loop.

However, what Marcia is referring to is that the correct way to do this would be to redirect requests with an added slash to remove the slash. In this way, you would avoid creating duplicate content -- The same page appearing at more than a single unique URL -- and a sure way to cause search engine ranking problems:


RewriteEngine on
#
# If filetype is present in requested URL, externally redirect to remove trailing slash
RewriteRule ^(.+\.[^/]+)/$ http://www.example.com/$1 [R=301,L]
#
# Internally rewrite x.php to index.php, unless URL-path is already rewritten to index.php
RewriteCond $1 !^index\.php$
RewriteRule ^(.+\.php)$ index.php?$1 [L]
#

Jim

sandeeppatil

2:53 pm on Jul 2, 2008 (gmt 0)

10+ Year Member



This work but the root where i placed .htaccess is having administrator directory at the same level.
Below is my directory structure
css
js
lib
administrator
index.php
login.php
search.php
.htaccess

But in the administrator directory(Administrator of site is here)
I have used URL like below
http://example.com/administrator/index.php?action=emailKeywords
http://example.com/administrator/index.php?action=messages

Which is having query string:
query strings after index.php -- like index.php?action=emailKeywords

This URL:: http://example.com/administrator/ having my administrator login.
so when i write http://example.com/administrator/ instead of administrator login ,i m getting redirect to site index page.

jdMorgan

3:16 pm on Jul 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Query strings have nothing to do with it -- By default, they are passed through mod_rewrite without change, and do not affect URL-path pattern-matching (this is because query strings are not part of a URL, they are data attached to a URL to be passed to the resource at that URL.

You may need to add "exclusions" to the second rule to prevent rewriting any "special" directory URL-paths, such as your administrator subdirectory:


# Internally rewrite x.php to index.php, unless URL-path is already
# rewritten to index.php or /administrator subdirectory is requested
RewriteCond $1 !^index\.php$
RewriteCond $1 !^administrator/
RewriteRule ^(.+\.php)$ index.php?$1 [L]
#

Jim

sandeeppatil

3:30 pm on Jul 2, 2008 (gmt 0)

10+ Year Member



Now its working but i used captcha images on the register page and
below is code of this
<img src="/CaptchaSecurityImages.php?width=180&height=35&characters=5" />
here i think due to parameters: ?width=180&height=35&characters=5
i m unable to view image .What to do it ?

jdMorgan

3:42 pm on Jul 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Again, nothing to do with query strings. Add another exclusion for the Captcha php file. I'm leaving the rest of this to you, as it is not my intention to write your code for you -- That would be called "consulting," not "dicussion".

For more information, see the threads in our Apache forum library, and the references cited in our forum charter. You will find the links at the top of every page in this forum.

Jim

sandeeppatil

3:52 pm on Jul 2, 2008 (gmt 0)

10+ Year Member



Sorry to disturb for small things.
Thanks Thanks Thanks for your valuable efforts.