"Folder of folder" in htaccess - php

I'm trying to make a .htaccess redirect to some webpages as usual.
Here's how I got it working:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^Profile/(.+)/?$ index.php?page=profile&username=$1 [NC,L]
RewriteRule ^profile/(.+)/?$ index.php?page=profile&username=$1 [NC,L]
RewriteRule ^Profile/Me index.php?page=profile [NC,L]
RewriteRule ^Profile/me index.php?page=profile [NC,L]
RewriteRule ^profile/Me index.php?page=profile [NC,L]
RewriteRule ^profile/me index.php?page=profile [NC,L]
RewriteRule ^Personagem/(.+)/?$ index.php?page=personagem&personagem=$1 [NC,L]
RewriteRule ^personagem/(.+)/?$ index.php?page=personagem&personagem=$1 [NC,L]
And here is what I need: When I'm on "Personagem" page, I can access all "Albums", on the following URL:
/Personagem/--name of character--/Album
Being, --name of character--, the parameter of /Personagem. Is it possible?

You can use:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/(.+)/?$ index.php?page=profile&username=$1 [NC,L]
RewriteRule ^profile/me index.php?page=profile [NC,L]
RewriteRule ^personagem/([^/]+)/?$ index.php?page=personagem&personagem=$1 [NC,L]
RewriteRule ^personagem/([^/]+)/([^/]+)/?$ index.php?page=personagem&personagem=$1&album=$2 [NC,L]
You can add other with:
RewriteRule ^personagem/([^/]+)/([^/]+)/([^/]+)/?$ index.php?page=personagem&personagem=$1&album=$2&other=$3 [NC,L]
No need to add PrOfILE or PROfILe... with NC (no case)

Related

.htaccess rewrite url to strip a single GET value

I am trying to rewrite my page like abc.com/profile.php?id=abc to abc.com/abc
Can you guide me?
When I try to add the following line, i getting 500 page error
RewriteRule ^([^/]*)$ /profile.php?id=$1 [L]
my htaccess
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /detail.php?type=$1&title=$2&id=$3 [L]
RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?pn=$1&type=$2 [L]
RewriteRule ^contact-us.html$ contact-us.php [NC,L]
RewriteRule ^about-us.html$ /about-us.php [NC,L]
RewriteRule ^privacy-policy.html$ /privacy-policy.php [NC,L]
RewriteRule ^legal.html$ /legal.php [NC,L]
RewriteRule ^search$ /search.php [NC,L]
RewriteRule ^disclaimer.html$ /disclaimer.php [NC,L]
RewriteRule ^submit-job$ /submit-job.php [NC,L]

htacces remove folder from custom url

I have the following code on my .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cp/artes-escenicas/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^cp/ciencia-y-tecnologia/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^cp/arte-y-diseno/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^cp/deportes/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^cp/idiomas/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^cp/academico/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^cp/rutas/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^cp/voluntariado/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
this allow me to have the following URL's:
http://pepperscope.com/cp/index.php
http://pepperscope.com/cp/artes-escenicas
http://pepperscope.com/cp/ciencia-y-tecnologia
http://pepperscope.com/cp/arte-y-diseno
http://pepperscope.com/cp/arte-y-diseno
http://pepperscope.com/cp/deportes
http://pepperscope.com/cp/campamentos
http://pepperscope.com/cp/idiomas
http://pepperscope.com/cp/academico
http://pepperscope.com/cp/rutas
http://pepperscope.com/cp/voluntariado
http://pepperscope.com/cp/lifestyle
But I need two things:
1) have the root on pepperscope.com instead of pepperscope.com/cp/index.php
2) remove the cp folder from all my links, like that:
http://pepperscope.com/artes-escenicas
http://pepperscope.com/ciencia-y-tecnologia
http://pepperscope.com/arte-y-diseno
http://pepperscope.com/arte-y-diseno
http://pepperscope.com/deportes
http://pepperscope.com/campamentos
http://pepperscope.com/idiomas
http://pepperscope.com/academico
http://pepperscope.com/rutas
http://pepperscope.com/voluntariado
http://pepperscope.com/lifestyle
Thanks in advance for your help.
You just need to remove the cp after ^, so something like this.
Remove all cp folders from all links
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^artes-escenicas/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^ciencia-y-tecnologia/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^arte-y-diseno/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^deportes/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^idiomas/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^academico/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^rutas/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
RewriteRule ^voluntariado/([^/]+)/?$ cp/course-details.php?name=$1 [NC,L]
To fix the problem with the root, best thing I can come up with is this
RewriteRule ^([/]?)$ cp/index.php

Hide directory name from URL by .htaccess

When user types:
www.domain.com/standard/Public/Modules/home.php or
www.domain.com/standard/Public/Modules/contacts.php
(or anything that follows the same pattern)
I want the URL to be displayed like this:
www.domain.com/home
www.domain.com/contacts
...
My current .htaccess:
RewriteEngine on
RewriteRule ^home.php/?$ standard/Public/Modules/home.php [NC,L]
RewriteRule ^contacts.php/?$ standard/Public/Modules/contacts.php [NC,L]
You need to have this in your root .htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} /standard/Public/Modules/login\.php [NC]
RewriteRule ^standard/Public/Modules/login\.php$ /home [L,NC,R=301]
RewriteCond %{THE_REQUEST} /standard/Public/Modules/logout\.php [NC]
RewriteRule ^standard/Public/Modules/logout\.php$ /contacts [L,NC,R=301]
RewriteRule ^home/?$ /standard/Public/Modules/login.php [NC,L]
RewriteRule ^contacts/?$ /standard/Public/Modules/logout.php [NC,L]
I use this, no need to individually set rewrites for pages
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
For example
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)-product(.*).html$ /products/view/$2 [L]
RewriteRule ^(.*)-cat(.*).html$ /products/listing/$2 [L]
RewriteRule ^products.html$ /products/listing/ [L]
RewriteRule ^bioproducts.html$ /products/bio_listing/ [L]
RewriteRule ^aboutus.html$ /static/index/15 [L]
RewriteRule ^contacts.html$ /static/contacts [L]
RewriteRule ^(.*)-partners(.*).html /partners/index/$2 [L]
</IfModule>

.htaccess rewrite help, remove .php from URL

I am not facing another problem with my mod_rewrite, first, time is my .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^news/([0-9]+)/?$ /news?id=$1 [L,QSA]
RewriteRule ^contact/([0-9]+)/?$ /contact?do=$1 [L,QSA]
RewriteRule ^account/([a-zA-Z]+)/?$ /account?action=$1 [L,QSA]
RewriteRule ^admin/([a-zA-Z]+)/?$ /admin?action=$1 [L,QSA]
RewriteRule ^login/([a-zA-Z]+)/?$ /login?action=$1 [L,QSA]
RewriteRule ^register/([a-zA-Z]+)/?$ /register?action=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L] <- This line doesn't work
RewriteRule ^([\w\d~%.:_\-]+)$ index.php?page=$1 [L,QSA]
All lines are working, beside the RewriteRule ^([^.]+)$ $1.php [NC,L]
and when I use this line, all other rules gets interrupted.
What am I doing wrong?
You need to use L flag in your problematic rule.
For removing the .php extension this is better rule:
Options +FollowSymlinks -MultiViews
RewriteEngine on
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
RewriteRule ^news/([0-9]+)/?$ /news?id=$1 [L,QSA]
RewriteRule ^contact/([0-9]+)/?$ /contact?do=$1 [L,QSA]
RewriteRule ^account/([a-zA-Z]+)/?$ /account?action=$1 [L,QSA]
RewriteRule ^admin/([a-zA-Z]+)/?$ /admin?action=$1 [L,QSA]
RewriteRule ^login/([a-zA-Z]+)/?$ /login?action=$1 [L,QSA]
RewriteRule ^register/([a-zA-Z]+)/?$ /register?action=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^(.+?)/?$ index.php?page=$1 [L,QSA]
Try something like
#Preventing loop
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
RewriteCond %{REQUEST_URI} !^.+\.php.*$
RewriteRule .* - [L]
Put this after RewriteRule ^register/([a-zA-Z]+)/?$ /register?action=$1 [L,QSA]

Confused about the way this .htaccess rule is supposed to work

I have this in my .htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^users/([^/]+)/?$ users.php?user=$1 [NC,L]
in my php I have
header("Location: /users/" . $_SESSION['user']);
it loads this into the browser
foo.bar/users/s2xi
which gives me a 404 Not Found error, I can understand up to the part where the directory users doesn't exist, but isn't my RewriteRule supposed to take care of that?
Edit: My complete .htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^foo\.bar$
RewriteRule (.*) http://www.foo.bar/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([a-zA-Z0-9_\s-]+)/([a-zA-Z0-9_\s-]+)(/)?$ users.php?type=$1&user=$2 [NC,R]
#RewriteRule ^users/?$ users.php [L]
RewriteRule ^([^/]+)/?$ profile.php?user=$1 [NC,L]
#RewriteRule ^users/([^/]+)/?$ users.php?user=$1 [NC,L]
#RewriteRule ^(admin)/(.+)/?$ users.php?user=$1 [NC,L]
RewriteRule ^admin/([^/]+)$ users.php?user=$1 [NC,L]
#error codes
RewriteRule ^/error/([^/]+)/ error.php?code=$1 [NC]
Change your .htaccess to the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^users/([^/]+)$ users.php?user=$1 [NC,L]
Also make sure users.php is in a right directory. If it's in root directory, you can use:
RewriteRule ^users/([^/]+)$ /users.php?user=$1 [NC,L]

Categories