LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 08-17-2010, 01:50 PM   #1
razzera
Member
 
Registered: Dec 2009
Posts: 105

Rep: Reputation: 15
apache2 rewrite rule help


(hope i posted in the right section)

Hello

In my webfolder on my server in the .htaccess file i have the following

RewriteEngine on
RewriteRule /([0-9]+) /index.php

and when i try to go to http://mypage.com/22 for example, it saying that the link is broken. why wont index.php be shown when i write a number after the / as in the example ?

Last edited by razzera; 08-17-2010 at 01:51 PM.
 
Old 08-17-2010, 02:13 PM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,174
Blog Entries: 1

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
Hi,

You don't need the leading slash "/" in the URI:
Code:
RewriteRule ([0-9]+) /index.php
Regards
 
Old 08-17-2010, 02:29 PM   #3
razzera
Member
 
Registered: Dec 2009
Posts: 105

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by bathory View Post
Hi,

You don't need the leading slash "/" in the URI:
Code:
RewriteRule ([0-9]+) /index.php
Regards
thank you, however i ran into another problem.

now going to http://mypage.com/22 gives me what i wants, however any number in my url do this now, if i where to go to http://mypage.com/subfolder/5.php it would point me back to the index.php in the rewrite rule, how can i make it not do that for subfolders and files in subfolders etc?

Last edited by razzera; 08-17-2010 at 02:33 PM.
 
Old 08-17-2010, 02:40 PM   #4
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
The rewrite rule is like so:
Code:
RewriteRule (\/[0-9]+) /index.php
The best way of going about it is probably editing the <Directory /> tag located in your httpd.conf

Code:
<Directory />
    Options FollowSymLinks
    AllowOverride None
    RewriteEngine on
    RewriteRule (\/[0-9]+) /index.php
</Directory>
If you don't know where your httpd.conf is located then search by:
Code:
find . -type f | grep httpd.conf
 
Old 08-17-2010, 02:48 PM   #5
razzera
Member
 
Registered: Dec 2009
Posts: 105

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by sag47 View Post
The rewrite rule is like so:
Code:
RewriteRule (\/[0-9]+) /index.php
When i added that to my .httacces file i get "this link is broken" if i go to http://mypage.com/22 but if i remove the / from index.php it works again however i cant use /22 anymore ofcourse.

Last edited by razzera; 08-17-2010 at 02:50 PM.
 
Old 08-17-2010, 03:04 PM   #6
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,174
Blog Entries: 1

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
You have to add a RewriteCond:
Code:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/[0-9]
RewriteRule ([0-9]+) /index.php
 
Old 08-17-2010, 04:17 PM   #7
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
Quote:
Originally Posted by razzera View Post
When i added that to my .httacces file i get "this link is broken" if i go to http://mypage.com/22 but if i remove the / from index.php it works again however i cant use /22 anymore ofcourse.
I said it's gotta go in httpd.conf in the <Directory /> tag. I didn't test it in .htaccess.

Maybe bathory's solution will work for you.
 
Old 08-17-2010, 04:39 PM   #8
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,174
Blog Entries: 1

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
Quote:
Originally Posted by sag47 View Post
I said it's gotta go in httpd.conf in the <Directory /> tag. I didn't test it in .htaccess.
The Rewrite stuff can go either in httpd.conf or in a .htaccess. I prefer the .htaccess method because there is no need to restart apache
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
httpd rewrite rule jnojr Linux - Server 5 01-25-2010 11:28 PM
Need help with apache Rewrite Rule? your_shadow03 Linux - Newbie 1 01-21-2010 07:12 AM
rewrite rule base url apache2 sinister1 Linux - Software 3 11-23-2007 10:17 AM
Apache rewrite rule help kinetik Linux - Software 1 11-05-2007 11:18 AM
i need a rewrite rule!help arcow Linux - Enterprise 1 07-03-2007 05:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 09:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration