petebolduc
petebolduc

Reputation: 1263

.htaccess returning mod_rewrite redirect to the index page of the www folder of wamp

I am trying to create "pretty urls for dynamically created pages. My desire is to render all pages from the index page.

I am working on my computer using wamp 2.5/apache 2.4.9

The apache httpd.conf is set to:

Listen 0.0.0.0:7080
Listen [::0]:7080
DocumentRoot "c:/wamp/www/"
ServerName localhost:7080

I have tried several different approaches which have resulted in everything from a blank page returning to a 404 error page. With the code below it returns to the index.php in the /www/ folder in wamp.

This is my non-working .htaccess code:

# Turn Rewrite Engine
Options +FollowSymLinks

RewriteEngine on

#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-l

# Pages 
RewriteRule ^([a-zA-Z0-9]+)$ index.php?topic=$1

# Rewrite to www.
RewriteCond %{HTTP_HOST} ^localhost:7080/demo [nc]
RewriteRule ^(.*)$ http://www.localhost:7080/demo/$1 [r=301,nc]

The clupret line of code is this I believe:

RewriteRule ^([a-zA-Z0-9]+)$ index.php?topic=$1

which I am thinking is some sort of path issue but am not sure... any assistance would be appreciated.

The URL in the address is: http://localhost:7080/demo/test where demo is the site folder in the wamp/www/

http://localhost:7080/demo/test/ (with the forward slash) returns a 404 error.

Upvotes: 1

Views: 608

Answers (2)

user5125586
user5125586

Reputation:

  1. Your htaccess is lacking a rewrite base. Just after: RewriteEngine on

    Add RewriteBase /demo/

  2. It is strictly not allowing trailing slashes Replace this line: RewriteRule ^([a-zA-Z0-9]+)$ index.php?topic=$1 withthis code:

    RewriteCond %{REQUEST_URI} !-f

    RewriteRule ^([a-zA-Z0-9]+)$ index.php?username=$1 [N,L]

This will allow urls ending trailing slashes to force back to your redirect url.

  1. Remove this from the last line:

    http://www.localhost:7080. Leave it as just: /demo/$1 [r=301,nc]

The second to the last line is not doing a lot. You can do and I will advice you do away with it.

Please do remember that when leaving the localhost environment for an online host, replace all incidence of "/demo/" with just "/"

Your final htaccess should be someting like this:

# Turn Rewrite Engine
Options +FollowSymLinks

RewriteEngine on

RewriteBase /demo/

#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-l
#RewriteCond %{REQUEST_FILENAME} !-d

# Pages 
#RewriteCond %{REQUEST_URI} !-f
RewriteRule ^([a-zA-Z0-9]+)$ index.php?username=$1 [N,L]

# Rewrite to www.
RewriteRule ^(.*)$ /demo/$1 [r=301,nc]

I hope this helps

Upvotes: 1

anubhava
anubhava

Reputation: 784888

Try these rules in /demo/.htaccess:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /demo/

# Rewrite to www.
#RewriteCond %{HTTP_HOST} ^localhost$ [NC]
#RewriteRule ^ http://www.%{HTTP_HOST}:7080%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?topic=$1 [L,QSA]

Upvotes: 0

Related Questions