Username with folder mod_rewrite - php

I have this problem, I have this url example.com/directory/fotos/index.php?u=username and what I need to do is change it to
example.com/username/fotos
I already have a rule in htacces which changes the directory to username in case a username is found in index.php
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ directory/index.php?u=$0&%{QUERY_STRING} [L]
Which ouputs example.com/someusername
How can I do a new rule for the above case without conflicting with the current rule?

This should do:
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/([^/]+)/fotos/?
RewriteRule ^([^/]+)/?$ /directory/index.php?u=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/directory/fotos/?
RewriteRule ^([^/]+)/fotos/?$ /directory/fotos/index.php?u=$1 [L,QSA]

Related

how to change htaccess rule to make url with query string without trailing slash

I want to change some rules in .htaccess file write now my url is like this www.example.com/d/somthing.htm and some another pages with www.example.com/category.htm now i want to change them in this way www.example.com/index.php?d=somthing what i have to do.
My current .htaccess content are:-
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_URI} ^/p\/[a-z][a-z]\/[[:print:]]+\.htm$
#RewriteRule \/p\/([a-z][a-z])\/([[:print:]]+)\.htm$ /index.php?p=$2&lang=$1 [L]
#RewriteRule \/p\/([a-z][a-z])\/([[:print:]]+)\.htm$ /index.php?p=$2&lang=$1 [L]
RewriteRule . /index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/p\/[[:print:]]+\.htm$
RewriteRule \/p\/([[:print:]]+)\.htm$ /index.php?p=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/s\/[a-z][a-z]\/[0-9]+\/.*$
RewriteRule \/s\/([a-z][a-z])\/([0-9]+)\/.*$ /category.php?action=something&value=$2&lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/s\/[0-9]+\/.*$
RewriteRule \/s\/([0-9]+)\/.*$ /category.php?action=something&value=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/n\/[a-z][a-z]\/[0-9]+\/.*$
RewriteRule \/n\/([a-z][a-z])\/([0-9]+)\/.*$ /novel.php?action=more&value=$2&lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^\/n\/[0-9]+\/.*$
RewriteRule \/n\/([0-9]+)\/.*$ /novel.php?action=more&value=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^\/sn\/[0-9]+\/.*$
RewriteRule \/sn\/([0-9]+)\/.*$ /novel.php?action=all&type=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^\/[a-z][a-z]\/[0-9]+\/.*$
RewriteRule \/([a-z][a-z])\/([0-9]+)\/.*$ /category.php?action=product&product=$2&lang=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^\/[0-9]+\/.*$
RewriteRule \/([0-9]+)\/.*$ /category.php?action=product&product=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^\/img\/[0-9]+\/[0-9]+\/.*$
RewriteRule \/img\/([0-9]+)\/([0-9]+)\/.*$ /img.php?value=$1-$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[0-9]+\+[a-z][a-z]\+[0-9]+\+.*$
RewriteRule /([0-9]+)\+([a-z][a-z])\+([0-9]+)\+.*$ /category.php?action=product&product=$1&lang=$2&something=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[0-9]+\+[a-z][a-z]\+.*$
RewriteRule /([0-9]+)\+([a-z][a-z])\+.*$ /category.php?action=product&product=$1&lang=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[0-9]+\+\+.*$
RewriteRule /([0-9]+)\+\+.*$ /category.php?action=product&product=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^\/img\/[0-9]+\/[0-9]+\/.*$
RewriteRule \/img\/([0-9]+)\/([0-9]+)\/.*$ /img.php?value=$1-$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[0-9]+\+[a-z][a-z]\+[0-9]+\+.*$
RewriteRule /([0-9]+)\+([a-z][a-z])\+([0-9]+)\+.*$ /category.php?action=product&product=$1&lang=$2&something=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[0-9]+\+[a-z][a-z]\+.*$
RewriteRule /([0-9]+)\+([a-z][a-z])\+.*$ /category.php?action=product&product=$1&lang=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[0-9]+\+\+.*$
RewriteRule /([0-9]+)\+\+.*$ /category.php?action=product&product=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/[0-9]+$
RewriteRule /([0-9]+)$ /category.php?action=product&product=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/s[0-9]+\+[a-z][a-z]\+.*$
RewriteRule /s([0-9]+)\+([a-z][a-z])\+.*$ /category.php?action=something&value=$1&lang=$2 [L]
</IfModule>
You can use these 2 rules in your root .htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^(?:d/)?(.+?)\.html?$ index.php?d=$1 [L,QSA,NC,R]

.htaccess add / support

RewriteEngine On
RewriteBase /myproject/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ $1.php [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^books/([^/]+)/$ books.php?id=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^books/([^/]+)$ books.php?id=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^books/ books.php [QSA]
Everything works when I type: localhost/name
but breaks if I type: localhost/name/
What's wrong with my .htaccess and how do I add support "/"
You don't need 2 rules and your 2nd rule has invalid regex anyway. Try this rule:
Options -MultiViews
RewriteEngine On
RewriteBase /myproject/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^books/([^/]+)/?$ books.php?id=$1 [L,NC,QSA]
RewriteCond %{DOCUMENT_ROOT}/myproject/$1\.php -f [NC]
RewriteRule ^([^/]+)/?$ $1.php [L]

.htaccess for post id

I have PHP file .htaccess :
Options -Multiviews
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?user=$1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/home/.*$ index.php [QSA]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/friends$ friends.php?user=$1 [L]
RewriteEngine On
RewriteRule ^post/(\d+)/([a-z-]+) post.php?id=$1&title=$2 [L]
Now I want the url post to be like this :
http://example.com/yoana/post/id_post
I tried with that rule above, but it is still not working.
The error is :
Object not found!
Any ideas?

php htaccess folders management

For now i have all requests to my domain to go through /index.php
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
However i want to add an exception. If the request goes to folder "/this_specific_folder/" then it should go for "/this_specific_folder/index.php" instead of "/index.php", without breaking the current existing logic.
How can i achieve this?
You can have it this way:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(this_specific_folder)(?:/.*)?$ /$1/index.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]

how to merge two htaccess files the first one is for the username and the second to hide php extension

i want to merge this .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?username=$1
with this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]
they are both in the root directory
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php?username=$1
RewriteRule ^([^\.]+)$ $1.php [NC]

Categories