htaccess rewrite username messing with pages - apache

I can access profiles using mysite.com/username but I cannot access mysite.com/home which originally is mysite.com/index.php?tab1=home.
Same problem with all other pages, for example, I also cannot access mysite.com/messages which originally is mysite.com/index.php?tab1=messages
I want to access both profiles and pages using the same link. mysite.com/profile & mysite.com/page.
This is the htaccess code I used.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/\/]*)$ index.php?tab1=profile&id=$1 [NC]
RewriteRule ^([^\/\/]*)/([^\/\/]*)(|\/)$ index.php?tab1=profile&tab2=$2&id=$1 [NC]
RewriteRule ^([^\/\/]*)/([^\/\/]*)/([^\/\/]*)(|/)$ index.php?tab1=profile&tab2=$2&tab3=$3&id=$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/\/]*)$ index.php?tab1=$1 [NC]

You have this rule:
RewriteRule ^([^\/\/]*)$ index.php?tab1=profile&id=$1 [NC]
Which has the pattern ^([^\/\/]*)$. So this matches "username", "home", AND "messages". That's why it's always going to the profile because that rule indiscriminately matches everything without a /.
Therefore, the last rule, which has the same pattern, will never match anything. You either need to make the profile a diffrent pattern, or hardcode the matches for "home" and "messages" and everything else that doesn't go to the profile.
So something like:
RewriteEngine on
RewriteRule ^(home|messages)$ index.php?tab1=$1 [L]
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/\/]*)$ index.php?tab1=profile&id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/\/]*)/([^\/\/]*)(|\/)$ index.php?tab1=profile&tab2=$2&id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/\/]*)/([^\/\/]*)/([^\/\/]*)(|/)$ index.php?tab1=profile&tab2=$2&tab3=$3&id=$1 [NC,L]
Or something like this maybe?
RewriteEngine on
RewriteRule ^profile-([^\/\/]*)$ index.php?tab1=profile&id=$1 [NC]
RewriteRule ^profile-([^\/\/]*)/([^\/\/]*)(|\/)$ index.php?tab1=profile&tab2=$2&id=$1 [NC]
RewriteRule ^profile-([^\/\/]*)/([^\/\/]*)/([^\/\/]*)(|/)$ index.php?tab1=profile&tab2=$2&tab3=$3&id=$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/\/]*)$ index.php?tab1=$1 [NC]
Making user profiles look like: mysite.com/profile-username

Related

.htaccess rewrite to add country code (if missing)

I am using a .htaccess file in the subdirectory /cms and using this subdirectory as RewriteBase. The redirections go to 'backend.php' and send the variables I want. It is a dual language site (nl|en), dutch and english. Everything works fine, as long as the %{REQUEST_URI} starts with (nl|en).
But I need a fallback to the default dutch language when nl|en is omitted, but can I add this to the following .htacces file. I have been trying and searching but cannot find the right syntax to make this happen:
RewriteEngine On
RewriteBase /cms/
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_URI} !(^(nl|en)/)
#RewriteRule (.*) This is where the solution should be used ?
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.[a-zA-Z]{2,4}$
RewriteRule ^(nl|en)/([^/]+)/$ backend.php?page=$2&language=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.[a-zA-Z]{2,4}$
RewriteRule (nl|en)(.*)/(\d+)/$ backend.php?page=$2&id=$3&language=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.[a-zA-Z]{2,4}$
RewriteRule (nl|en)(.*)/(\d+)/(.+)/$ backend.php?page=$2&id=$3&task=$4&language=$1 [QSA,NC,L]
One way to handle this is a rewrite with the dutch language prepended. This will silently change direction to the proper page.
RewriteCond %{REQUEST_URI} !^/cms/(?:nl|en)/
RewriteRule ^(.*)$ nl/$1 [L]
If you want the client to notice and change the URL, you must do a R|redirect instead
RewriteCond %{REQUEST_URI} !^/cms/(?:nl|en)/
RewriteRule ^(.*)$ nl/$1 [R,L]
Your default rule can be this one:
# This is where the solution should be used ?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(nl|en)/ [NC]
RewriteRule ^([^/]+)/?$ backend.php?page=$1 [QSA,L]

Dynamic htaccess code for multiple php parameters to friendly urls

I have searched and searched for an answer to this, but none of the posts I've found on stackoverflow work for me - nor do the online htaccess generators.
I have made slight adjustments to Anubhava's excellent answer on htaccess redirect for dynamic urls not working to suit different page names as follows:
RewriteEngine On
# for external redirection from `/hp.php?su=sitename` to `/sitename`
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+hp\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# for internal redirection from `/sitename` to `/hp.php?su=sitename`
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /hp.php?su=$1 [L,QSA]
However, in addition to sending users to domain.com/sitename (which the modified code above does) I also want to change the following urls.
domain.com/newpage.php?su=sitename&PgID=1234&pu=pagename
to become
domain.com/sitename/1234/pagename.html
similary
domain.com/diary.php?su=sitename
to become
domain.com/sitename/diary.html
This last one would be replicated for similar dynamic pages, such as
future.php?su=sitename >> domain.com/sitename/future.html
photos.php?su=sitename >> domain.com/sitename/photos.html
etc
Some time in the future, I would also like to divert http:// to https:// - would this rule go before all the others?
Hope somebody can help
UPDATE:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/hp\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/newpage\.php\?su=([^\s&]+)&PgID=(\d+)&pu=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(diary|future|photos)\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%2/%1.html? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /hp.php?su=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(diary|future|photos).html$ /$2.php?su=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(\d+)/(.+)\.html$ /newpage.php?su=$1&PgID=$2&pu=$3 [L,QSA]
It isn't all that difficult to derive from the previous rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/hp\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/newpage\.php\?su=([^\s&]+)&PgID=(\d+)&pu=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(diary|future|photos)\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%2/%1.html? [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /hp.php?su=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(diary|future|photos).html$ /$2.php?su=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(\d+)/(.+)\.html$ /newpage.php?su=$1&pgID=$2&pu=$3 [L,QSA]
As to your second question about http to https, yes; that particular redirection should occur before all others.

How to create clean url using .htaccess for multiple parameters?

I try to make clean URLS to my web site.
The desired format is: site/category/num example site/sport/3
Both category and page are optional parameters. So i have three rules. The problem is rule three. It only work in first time.
User enter site - tule 1. OK.
User navigate and pick category. Rule 2... OK.
User navigate and want page 2 page. (rule 3) OK.
User want see more items in page 3 (rule 3) Error..
I got parameter site/category/category/3 and navigatioin fail. Now i dont get why do i get category two times.
My .htaccess file work almost.
RewriteEngine On
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)$ /index.php?page=$1 [L]
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?category=$1 [L]
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)$ index.php?category=$1&page=$2 [NC,L]
Try with:
RewriteEngine On
RewriteCond %{REQUEST_URI} (\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC,OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([0-9]+)/?$ /index.php?page=$1 [L]
RewriteRule ^([^/]+)/?$ /index.php?category=$1 [L]
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)/?$ /index.php?category=$1&page=$2 [NC,L]

RewriteRule to redirect whole domain to get parameter

I want to redirect a whole url to a query parameter with a RewriteRule in .htaccess
for example: http://server.com/http://google.com should be redirected to
http://server.com/index.php?url=http://google.com
so far i'm just able to make this work: http://server.com/google.com but when a : or / is contained, it doesn't work..
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z0-9_.-]+)$ index.php?url=$1 [L,NC,QSA]
thanks for help!
RewriteRule patter strips multiple / into one, better use RewriteCond here:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . index.php?url=%1 [L,NC,QSA]
Why not just do the TLD and then add the http:// in the rule. This is how I would do it.
This is the way I would use it so it doesn't "look" invalid. http://server.com/google.com
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z0-9_.-]+)$ index.php?url=http://$1 [L,NC,QSA]

RewriteRule - folder exceptions (with dashes)

I have drupal installed in my website root with a directory called xi-admin underneath it, that directory has a .htaccess inside doing password protection. problem is, is that it is rewriting http://www.example.com/xi-admin/ back to index.php????
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(xi-admin|xi-admin/.*)$
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Try changing
RewriteCond %{REQUEST_URI} !^(xi-admin|xi-admin/.*)$
to
RewriteCond %{REQUEST_URI} !^/xi-admin
In your configuration, you are referring in one rule to foo, in the other to /bar, one of them will never match.
Either choose:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(xi-admin|xi-admin/.*)$
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Or another alternative using relative addressing, which I would recommend because it works even if this directory would later be moved to somewhere else in the directory hierarchy:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(xi-admin|xi-admin/.*)$
RewriteCond $1 !=favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]