homepage Welcome to WebmasterWorld Guest from 184.73.87.85
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

This 117 message thread spans 4 pages: < < 117 ( 1 2 [3] 4 > >     
URL rewrite help needed
nevski




msg:4595721
 12:52 pm on Jul 23, 2013 (gmt 0)

Hi this is my first post on this forum.
The content of this forum is great,so keep up guys!

I have a problem with url rewriting.

This is my test page that I'm using for this purpose:
http://example.com/

I want to rewrite url:

http://example.com/index.php?page=about

to

http://example.com/about



or to be more precise to rewrite any page that contains
index.php?page=
to
http://example.com/$GET
variable name.

Content of .htaccess file is:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1


The problem is that url rewrite doesn't change in address bar it stays the same when I access to it by link on my navigation bar the link is:

<li><a href="index.php?page=about">About</a></li>


Adress bar:
http://example.com/index.php?page=about


But when I type manualy in address bar

http://example.com/about


it opens page correctly

So where is my mistake?
Any help guys?I would appreciate it!Tnx in advance!

[edited by: phranque at 2:24 pm (utc) on Jul 23, 2013]
[edit reason] Please Use Example.com [webmasterworld.com] [/edit]

 

nevski




msg:4597853
 11:19 pm on Jul 30, 2013 (gmt 0)

First of all I'm using the Xampp version 3.2.1.I've substituted the older version of Xampp because I though that the older version causes the problem,but the problem remained.

To access the website on my localhost I just type localhost/folder(that contains the website)/ and without problems it loads the page.Links are loaded normally also.

When in rewrite rule is only flag L the pages load normaly,like when I type name other page that I have in rewrite rule.

The problem beggins wih R=301 flag it redirects the page and says connection with localhost:blahbalh can't be established.No matter what is written in substitution (localhost/%1?,or only /%1?).Only when i write http://localhost/%1? it normaly shows it in address bar like localhost/%1? but with 404error...

I didn't put +FollowSymLinks in any of mine .htaccess files...

[edited by: phranque at 11:31 pm (utc) on Jul 30, 2013]
[edit reason] unlinked url [/edit]

g1smd




msg:4597857
 11:29 pm on Jul 30, 2013 (gmt 0)

Try adding
Options +FollowSymLinks at the beginning.

What happens if you replace
/%1? with http://localhost/%1? or with http://127.0.0.1/%1? in the htaccess file?

Add a blank line after each RewriteRule.

[edited by: g1smd at 12:35 am (utc) on Jul 31, 2013]

nevski




msg:4597870
 12:06 am on Jul 31, 2013 (gmt 0)

When I add +FollowSymLinks and try to access testing website it shows error500...I didn't changed the other things that were written since the first thing didn't work.

If I replace with http://localhost/%1? or http://127.0.0.1/%1? it shows 404 error(thats' without +FollowSymLinks).

g1smd




msg:4597873
 12:35 am on Jul 31, 2013 (gmt 0)

Oops. I missed out the preceding word "Options". Previous post corrected. Try again.

What exact URL is shown in the browser address bar when you see the words "404 error"?

nevski




msg:4597957
 8:33 am on Jul 31, 2013 (gmt 0)

I've entered full code Options +FollowSymLinks still all remains the same.

Only when I enter http://localhost/%1? it shows that kind of error(404 error)

It says the requested URL wasn't found on this server.The link on the referring page seems to be wrong or outdated.

With anything other it says it that connection to the server failed and shows.

localhost:1337/(example variable)

lucy24




msg:4597973
 10:24 am on Jul 31, 2013 (gmt 0)

Only when I enter http:/ /localhost/%1? it shows that kind of error(404 error)

Urk. What do you mean "when I enter"? That makes it sound as if you're typing in the literal text "%1". What does the browser's address bar say? If you're talking about redirects created by RewriteRules, the address bar absolutely should not say %1; it should display some URL or other. Or at worst, if nothing was captured, it would send you to the root.

With anything other it says it that connection to the server failed and shows.

localhost:1337/(example variable)

/localhost/
creates a valid server connection, though the file may not exist.

localhost:1337/
isn't recognized as a valid server.

Where is the :1337 in the URL coming from?

Can you otherwise browse your local site? Can you follow internal links when they don't get redirected? What does your browser then say for the hostname?

nevski




msg:4597991
 12:17 pm on Jul 31, 2013 (gmt 0)

RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} page=([a-z]+)
RewriteRule ^index\.php$ /%1? [R=301,L]
RewriteRule ^([a-z]+)$ /index.php?page=$1 [L]


This is the code that I implemented,it works without problems on web testing server.

On localhost is shows error like on this picture

[support.cdn.mozilla.net...]

except the name of server is localhost:1337 that's all,and no matter what I've tried it remains the same.So if the same rule works properly on the web server,I guess something is wrong with my local settings.

localhost:1337/aboutthat is url shown in a browser,and the browser looks like on that picture.

lucy24




msg:4598212
 9:10 pm on Jul 31, 2013 (gmt 0)

What does the address bar say when you are using WAMP (or was it XAMP?) successfully? Can you follow any links within your local site?

This is starting to seem more like a _AMP problem rather than a mod_rewrite problem.

Fortunately it's the same subforum either way ;)

nevski




msg:4598233
 9:47 pm on Jul 31, 2013 (gmt 0)

I'm using Xampp ,maybe I can try another program?Can you recoment me some other to try?

It does redirecting in adress bar but it doesn't display content.

For example when I eneter the page,the home page opens correctly,when i click on link it redirects and shows that it could not connect to server....

lucy24




msg:4598254
 11:11 pm on Jul 31, 2013 (gmt 0)

Comment-out your redirects. Now do the links work? If the page doesn't physically exist, you'll get a 404-- but if everything else is working correctly, you will still see your custom 404 page. That's assuming you've included that part in your XAMPP site.

Don't talk to me about _AMP. My sql server crashed and refused to start-- seems to have something to do with setting up a RewriteLog-- and all the documentation is in Hungarian. In the end I gave up, re-downloaded MAMP and reinstalled from scratch. Turns out there's a newer version anyway, and luckily I don't use sql for anything.

nevski




msg:4598361
 9:45 am on Aug 1, 2013 (gmt 0)

I've browsed a little through config file of my Apache server.

ServerName localhost:1337

#
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
AllowOverride All
Require all denied
</Directory>


Now I've tested on my browser when I type in address bar localhost/root_folder_of_localhost_site/ the browser opens the page like charm.

But when I type localhost:1337(that's the name of server in config Apache file)/root_folder_of_localhost_site/ it says that he can't establish connection with a server.So as I noticed the server does R=301 through localhost:1337,so it might be a problem.Since he can't open my website on local I guess this problem might be in connection with this setup.

Would it be harm to erase localhost:1337 and put just loclahost in Apache config file?

Tnx guys once again for effort!

g1smd




msg:4598365
 10:11 am on Aug 1, 2013 (gmt 0)

when I type in address bar localhost/root_folder_of_localhost_site/

Are you typing "exactly" that or is there a
http:// somewhere in there too?

Use the [ code ] and [ /code ] tags around code and URLs to stop the linking process in this forum.

lucy24




msg:4598378
 11:52 am on Aug 1, 2013 (gmt 0)

Would it be harm to erase localhost:1337 and put just loclahost in Apache config file?

Do it through XAMPP's prefs. If it's anything like MAMP there's a tab for "Ports".

If you do anything whatsoever to the config file-- where "anything whatsoever" means moving a comma or adding a comment line-- first make a copy. And, of course, stop the server before editing the config file. It isn't like htaccess where you can make changes on the fly and then just reload a page.

nevski




msg:4598384
 12:36 pm on Aug 1, 2013 (gmt 0)

@g1smd

I just type
localhost/test_folder_name/.Without http://

@lucy 24

Now I've chagned loclahost:1337 to just localhost and finaly it doesn't says there is no connection to server,but it shows 404 error.

When I enter the site, it opens it correctly and when i click the links it redirects me to desired url,that is what I see and it is displayed on address bar.But the content won't load.There are no prolems with [L] flag,just with R=301 flag.I've commented all and tried the miltiple options with that,but still when there is falg R=301 it shows 404error....

lucy24




msg:4598556
 10:52 pm on Aug 1, 2013 (gmt 0)

Frankly I'd throw up my hands and simply reinstall XAMPP :)

JD_Toims




msg:4598558
 10:56 pm on Aug 1, 2013 (gmt 0)

Frankly I'd throw up my hands and simply reinstall XAMPP :)

I'd throw XAMPP in the trash and use a live subdomain called testing or sandbox or xampp-sucks-so-i-use-this-live-site-subdomain-now.

nevski




msg:4614214
 11:15 am on Oct 2, 2013 (gmt 0)

Hi guys,

I wish to thank you for great effort and all the things that you are doing here.It was great help for me and attention that I got here.I recomended this forum to many people as the best place for learning.

I didn't had a time to thank you for all things.So this is a little bit late gratitude,still you deserve all the praise.Sincerely!

nevski




msg:4615210
 10:35 pm on Oct 7, 2013 (gmt 0)

Hi guys,

First of all I would like to put banner of your website to my website.This is the only way in which I could show gratitude for your work here.So ifyou have one like 200px in width just send it to me...

Second I'm building one project,that concerns huge amount nice clean urls.Like title of news and something like that.

So I'm wondering how is possible to do this kind of thing.

For example:

I want all my urls to be transformed from

www.example.com/news.php?id=1 to

example.com/news/title-of-news


So I guess it must me done both in php,and mod_rewrite.

So in your opinion what is the best way to do that.
Before I start to do something,the plan in needed,and advice from experts would be great deal for me.

P.S.Your advices about learning mod_rewrite proved very very good.

lucy24




msg:4615253
 3:32 am on Oct 8, 2013 (gmt 0)

I would like to put banner of your website to my website.

Whoops! Have you any idea how close I was to running off in search of a moderator because I read only every other word and it came out like spam?

So I'm wondering how is possible to do this kind of thing.

Well, we're not going to do it for you :) But it can certainly be done. That faint sound you hear in the distance is g1 getting excited over the prospect of replacing long old ugly URLs with new short pretty ones. If there's no visible relationship between old URL and new URL, and there are lots and lots of them, you're looking at a detour to php.

g1smd




msg:4615272
 8:16 am on Oct 8, 2013 (gmt 0)

Luckily there have been two threads in the last few weeks, both with copious amounts of example code showing how this can be done. They are recommended reading to get you started.

You're recommended to use a URL that looks like
example.com/n1-title-of-news where n1 means news item number one.
nevski




msg:4615350
 2:48 pm on Oct 8, 2013 (gmt 0)

Glad to see you all!Tnx from promt responses.

Well Lucy24,it't not such a hurry cause I'm starting this web site from scrach.So there is no content or what so ever.Just it think is wise to have plan of developing.It's not such a hurry.Better to take some time and do it on right way,then punching my head later with problems which may occure.

G1SMD you wrote:

example.com/n1-title-of-news where n1 means news item number one


n1 should be a GET variable or id from MYSQL database?

I have one question kind of concerning off topic,but is connected to it.

What is better and what is better for SEO?

example.com/cats/some-cat

or just
example.com/some-cat


I will read these posts that you've recommended=)

nevski




msg:4615418
 6:37 pm on Oct 8, 2013 (gmt 0)

Also I've been trying something

On the page when I'm extracting data from databse i've setup this.

$abc=$row['example_text'];
Then I've made a variable:
$href=str_replace(" ","-","$abc");


and placed it in

echo "<a href=".$href.">Link</a>";


I've made a rewrite rule.

RewriteEngine on
RewriteCond %{THE_REQUEST} [A-Za-z0-9]\ /Example-Text\ HTTP/
RewriteRule ^Example-Text$ /text.php [L]


Where is my mystake,and is it good idea using str_replace?

lucy24




msg:4615461
 9:26 pm on Oct 8, 2013 (gmt 0)

example.com/cats/some-cat

This form implies
cats/other-cat
cats/mycat
cats/neighbors-idiot-cat

alongside
dogs/thisdog
dogs/thatdog
dogs/worlds-best-dog-that-i-had-as-a-child

et cetera. If it's only one page per category, you don't need the category. Arrange your URLs as if they were physical directories. It doesn't matter if behind the scenes it's all

RewriteRule ^paintings/(spare[cr]ats|misc|pending)/(\w+)\.html /paintings/misc/misclinks.php?subdir=$1&page=$2 [L,NS]
nevski




msg:4615605
 10:25 am on Oct 9, 2013 (gmt 0)

Well my idea is to make webpage that will have few categories.

It's not a hurry,because it's my project so I don't have time limit or what so ever.Just wan't to do things right)

I've made simple testing page for this kinds of thing.

First is just

somepage.php?id=1 --->to somepage/example-page

or

using str_replace to get page title or name,create a link that sends us to desired page and then in mod_rewite to make a rule.

Like convert

somepage/example-page--->somepage.php?text=example-page

So is second example a good solution or not?

Thank you very much for the effort!I appreciate it!

lucy24




msg:4615740
 10:31 pm on Oct 9, 2013 (gmt 0)

Like convert

somepage/example-page--->somepage.php?text=example-page

So is second example a good solution or not?

What do you mean by "convert"? I'm assuming that

somepage/example-page

is your visible URL, and behind the scenes this rewrites to serve content from

somepage.php?text=example-page

Is that what you mean? It may be better if "somepage" isn't the same literal text in both places-- that is, a pseudo-directory in an URL, and also a physical file. But this takes us into "Do as I say, not as I do" territory, because until recently I had a whole directory like this:

/paintings/cats/lots-of-files-here
/paintings/cats.html
/paintings/rats/lots-of-files-here
/paintings/rats.html

and so on. I only recently bit the bullet and changed everything (both URLs and physical filenames) to

/paintings/cats/lots-of-files-here
/paintings/cats/
/paintings/rats/lots-of-files-here
/paintings/rats/

et cetera. But I only did this after I'd replaced the hand-rolled navigation with a simple php include so I didn't have to edit every single individual file within (I think) eleven directories.

:: detour to check ::

Oops, no, it's fourteen. Whew. Still better than editing 200 individual pages :)

nevski




msg:4616026
 10:29 pm on Oct 10, 2013 (gmt 0)

First of all sorry becuase my bad explanation.

Yes,my idea was to create behind scenes RewriteRule for this.But I'm troubled because I would maybe have for example hundreds of pages,and if I must create RewriteRule for every page it could be a problem.

For example for

somesite.com/cat-food to be index.php?title=dog-food
somesite.com/dog-food to be index.php?title=dog-food

So my question is there a way in mod_rewirite to make a RewriteRule that say ok

take pattern after / for example dog food and then place it index.php?title=dog-food

or food/dog-food also take it and make food.php?title=dog-food

?

lucy24




msg:4616031
 10:55 pm on Oct 10, 2013 (gmt 0)

somesite.com/cat-food to be index.php?title=dog-food
somesite.com/dog-food to be index.php?title=dog-food

Did you mean those examples literally? If so, it's a single rule:

RewriteRule ^([^.]+)$ /index.php?title=$1 [L]

For safety, this rule should be preceded by the generic redirect in the other direction:

RewriteCond %{THE_REQUEST} title=([^&]+)
RewriteRule ^index\.php http://www.example.com/%1? [R=301,L]

If this is a brand-new site that has never used long URLs with query string, you may be able to dispense with the redirect. But check your logs periodically, because you never know. Not long ago, one of my test sites was hit with a flurry of explicit requests for /forbidden.html. Ours not to reason why, ours but to redirect or die. Shrug.

nevski




msg:4616126
 9:55 am on Oct 11, 2013 (gmt 0)

Tnx Lucy24 for advice.

I will surely not put this site on-line for a while,it's not a hurry at all,just I want to get things wight,to test it and if it shows some kind of funny activity.I would have a period of testing on the server.

It's nice to have you guys you are such a help,and I'm really really grateful for it!

I will write down the results here after testing.

nevski




msg:4616239
 10:32 pm on Oct 11, 2013 (gmt 0)

It works like charm,really great))Still I will test it for a while.

I've tryed one more option but somewhere it stucks.

Basiclly I added on php page

something like this

$href=str_replace(" ","-",$row['title']);
echo "<a href=category/".$href." >".$row['title']."</a><br/>";

So it showed like desired results like

category/some-name


I've changed

RewriteCond %{THE_REQUEST} title=([^&]+)
RewriteRule ^index\.php http://www.example.com/%1? [R=301,L]
RewriteRule ^category/([^.]+)$ /index.php?title=$1 [L]

So it showes desired content but without CSS.I've made direct link to CSS link on this page www.example.com/style.css I checked it and it is proper one and still there is no result concerning CSS.So I'm bit puzzled?I'm sure that I did something wrong.

lucy24




msg:4616254
 12:43 am on Oct 12, 2013 (gmt 0)

What does "direct link" mean? If you're rewriting pages, you have to make sure all supporting files use absolute links-- the ones that start in / slash.

A browser doesn't know that a rewrite has taken place. That's the essence of a rewrite. So if it "thinks" it's at
www.example.com/category/newpage
when it's "really" at
www.example.com/index.php?title=newpage
then if the HTML on the page refers to
mystyles.css
(without leading slash) then the browser will ask for
www.example.com/category/mystyles.css

Further complications ensue when an external stylesheet in turn references a background image, because then you have to remember whether those image URLs are relative to the page or to the CSS if they're in different directories. (I have a permanent mental block on this detail.)

JD_Toims




msg:4616276
 4:18 am on Oct 12, 2013 (gmt 0)

What Lucy24 said, and, for as long as I've been online people who code and actually build sites beyond WYSIWYG editors have recommended against directory-relative URLs [the-path-to/the-file.ext] in-favor-of server-relative URLs [/the-path-to/the-file.ext] due to the exact issue you're stating.

Personally, even if it took some time I'd change everything over to have a server-relative location [starts with a /], because when you do these issues don't pop-up and there are many less headaches and unexpected situations involved, especially if you get into complex rewriting and redirecting.

BTW: There are some programs that can use regular expressions to read closed files and do a find/replace [I use TextWrangler on a Mac, but DreamWeaver does the same thing -- not sure about others] so replacing every occurrence can be as easy as: Find==href="(([^/]{1})|https?://) : Replace==href="/\2 : Select the directory the site is in and "replace all".

This 117 message thread spans 4 pages: < < 117 ( 1 2 [3] 4 > >
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved