mod_rewrite loop issue with usernames - php

I am using mod_rewrite to make profile page usernames as in mydomain.com/johndoe
The code below works just fine until I try going to a directory like www.mydomain.com/images, upon which I get thrown into an infinite loop to http://www.mydomain.com/images/?username=images
Current Code:
RewriteEngine On
# Add WWW
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Redirect usernames
RewriteRule ^([A-Za-z0-9-]+)?$ profile.php?username=$1 [L]
I also tried this, but it doesn't work either:
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^([A-Za-z0-9-]+)?$ profile.php?username=$1 [L]
Where am I going wrong?

The problem is, that you have to decide wether it's an existing directory, or a fake uri which contains a username or other actions which gonna call some php-scripts or something.
The way with !-d is right, but there is also !-f for files.
Try something like
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^([A-Za-z0-9-]+)?$ profile.php?username=$1 [L]

Related

How to improve this .htaccess file?

I created the following .htaccess file and I am wondering if it could be improved:
RewriteEngine On
RewriteRule ^site$ ./site/ [L,NC]
RewriteRule ^site/(.*)$ ./main/$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/en [NC]
RewriteRule ^(.*)$ ./404.php?url=$1 [L,NC]
I want to redirect visitors which enter www.domain.com/site/ to www.domain.com/main/ and all other requests to www.domain.com/404.php?url=... This works, except when the user doesn't enter a trailing slash (www.domain.com/site).
You should be able to do that with a /? like
RewriteEngine On
RewriteRule ^site$ ./site/? [L,NC]
RewriteRule ^site/(.*)$ ./main/$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/en [NC]
RewriteRule ^(.*)$ ./404.php?url=$1 [L,NC]
For the first rule, you rewrite site to site/ and then rewrite site/ to main/. I would rather do it in one step, e.g.
RewriteRule ^site/?$ ./main/ [L,NC]
The second rule would stay as it is.
With the current rules, you only redirect nonexisting paths to 404.php. If this is what you want, everything is fine. If not, you should remove !-f and !-d conditions and add a condition to exclude main from being rewritten to 404
RewriteCond %{REQUEST_URI} !/main/ [NC]
Even though the substitution ./404.php is valid, I would write it as
RewriteRule ^(.*)$ 404.php?url=$1 [L,NC]

How to rename part of a url with .htaccess?

I currently have very little Apache experience, and am having difficulties with my .htaccess file. My question is this: how can I rename these files, listed below, properly? I believe my syntax is accurate, according to http://www.htaccesscheck.com, but when accessing these pages, either A: the page won't load due to a redirect loop, or B: the page won't load, but will redirect to the wrong page. Here is my current .htaccess file for this directory:
RewriteEngine On
RewriteBase /photos/
RewriteRule ^(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^(.*)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
Any help is much appreciated.
Try code below:
RewriteEngine on
RewriteBase /photos/
RewriteRule ^([0-9]{1,2})-([0-9]{4})$ archives.php?month=$1&year=$2 [L]
RewriteRule ^([0-9]+)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
This rules will check, if:
request like yourdomain/11-1111, then return archives.php
request like yourdomain/111, then return catpost.php (you can type
any number)
else will return viewpost
You have some errors in your current .htaccess, because your second rule get result of first rule.
By the way, you can use http://htaccess.madewithlove.be/ to check step by step what is posted to your rewrite rules.
be sure to write a valid pattern
try this
RewriteEngine On
RewriteBase /photos/
RewriteRule ^(.*?)-(.*?)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^(.*?)$ catpost.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*?)$ viewpost.php?id=$1 [QSA,L]

How to exclude main directory/home page from rewriting rule in htaccess?

I can not figure out how to exclude home page from rewriting.
In other words, .htaccess treats "example.com" as "example.com/user/index.php?name=" so it displays user specific content instead of my main page. I've tried bunch of things and nothing worked as I expected.
RewriteEngine on
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^about/([A-Za-z0-9-]+)/?$ /about/index.php?p=$1 [L]
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteCond %{REQUEST_URI} !^/login/?$ [NC]
RewriteCond %{REQUEST_URI} !^/register/?$ [NC]
RewriteCond %{REQUEST_URI} !^/about/?$ [NC]
RewriteRule ^([A-Za-z0-9.]+)/?$ /user/index.php?name=$1 [L]
Hopefully you guys understand what my problem is.
Rules look fine, the + qualifier in your regex should make it so it won't match just the / request. You can maybe try adding this right below RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+(?:index\.php|)($|\ )
RewriteRule ^ - [L]

CodeIgniter and Old-style PHP

I have a domain, let's say somedomain.com. I have an admin page somedomain.com/admin. The pages were coded in plain PHP. I recently re-wrote somedomain.com in CodeIgniter. But I do not want to touch somedomain.com/admin for now.
How do I make this work? When I go to somedomain.com/admin, it is looking an admin controller, which of course does not exist. I have an index.php file in admin folder, I cannot figure out a way to access the index file.
Thank you.
You should post .htaccess for us to se. But anyway. You should exclude "admin" to be directed to index.php
something like this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteCond %{QUERY_STRING} ^(ACT=.*)$ [NC]
RewriteRule ^(.*)$ index.php?%1 [L]
RewriteCond $1 !^(admin|index.php) [NC]
RewriteRule ^(.*) /index.php?/$1 [L]
Like this :
RewriteEngine On
RewriteBase /
RewriteCond $1 !^(index\.php|assets|thirdparty|admin|themes|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

htaccess: different url for same page doesn't work

I've got these lines in htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [NC]
RewriteRule ^(.*)/(.*)$ index.php?page=$1&article=$2 [NC]
when it comes to address like this /news/test, page variable would be like index.php.
any idea how to fix that?
You have (.*) matching in first row which will match for both your scenarios.
Try this out it worked for me in following scenarios:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /index.php?page=$1&article=$2 [L]
RewriteRule ^(.*)$ /index.php?page=$1 [L]
It worked for:
http://www.example.com/news/test -> http://www.example.com/index.php?page=news&article=test
http://www.example.com/news/ -> http://www.example.com/index.php?page=news&article=
http://www.example.com/news -> http://www.example.com/index.php?page=news
Let me know, if you need to do something else. To test it properly i have added [L,R=302] instead of [L] to see if URL is forming correct or not.

Categories