.htaccess /subdirectory/ -> /subdirectory/index - .htaccess

I'm sorry if the topic with same question already exists, but I couldn't find it out.
So. My problem is, that I've a webpage in subdirectory; like..
/var/www/example/
Now, what I really need:
when I try, to get on the site from URL like: www.example.com/examplesubdir/ ; dirname($_SERVER['REQUEST_URI']) isn't returning anything.
That's why I want to rewrite it from www.example.com/examplesubdir/ to www.example.com/examplesubdir/index
My current .htaccess file looks like this:
Options -Indexes +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9]{18})_([A-Za-z0-9]{3,4})$ show_file.php?f=$1&t=$2 [L,QSA]
RewriteRule ^([A-Za-z0-9]{18})$ show_file.php?f=$1 [L,QSA]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Thank you, and appreciate any help.

RewriteCond is only applicable to next RewriteRule only.
Replace your /var/www/.htaccess code with this:
Options -Indexes +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([A-Za-z0-9]{18})_([A-Za-z0-9]{3,4})$ show_file.php?f=$1&t=$2 [L,QSA]
RewriteRule ^([A-Za-z0-9]{18})$ show_file.php?f=$1 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [NC,L]

Related

Query String to Path

Hi I currently have this as my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/(assets|modules|plugins)/ - [NC,L,QSA]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -s
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} -d [OR]
RewriteRule (.*) - [NC,L,QSA,S=3]
RewriteRule ^/(.*)/(.*)$ /$1?type=$2 [NC,QSA]
RewriteRule ^/(.*)$ /index.php?page=$1 [NC,L,QSA]
</IfModule>
This was moved from an apache vhost config to .htaccess but I am getting 404's.
For example: I would like this page http://stripsync.com/index.php?page=venue to look like this http://stripsync.com/venue
What am I doing wrong? All help is greatly appreciated!!
Change it to this, removing opening forward slashes since you moved it to .htaccess (which doesn't include them in the match), and removing %{DOCUMENT_ROOT} since %{REQUEST_FILENAME} already has it in this context.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(assets|modules|plugins)/ - [NC,L]
RewriteCond %{ENV:REDIRECT_REWRITTEN} =1 [OR]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(.*)/(.*)$ /$1?type=$2 [QSA,L,E=REWRITTEN:1]
RewriteRule ^(.*)$ /index.php?page=$1 [QSA,L,E=REWRITTEN:1]
</IfModule>
I removed unused flags and capturing.

Rewrited URL giving 404 error

I'm using mode rewrite to make seo friendly urls. Please see the code below
Options +FollowSymLinks
RewriteEngine on
RewriteRule discover/([0-9]+)$ discover.php?page=$1 [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Code work in my local host but in the hosting server it doesn't work.
If i added with the file extension it works in the server but without the extension it doesn't work.
Ex: below works
RewriteRule discover-([0-9]+)\.html$ /discover.php?page=$1 [L]
Any pointers on how to make the 1st code it work? Appreciate your help.
Try this too,
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^discover/([0-9]+)$ discover.php?page=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Turn off MultiViews option and check for existence of .php file before adding .php in a request:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule discover/([0-9]+)/?$ discover.php?page=$1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Redirect to other location

I have the current setup where my file system looks like this
/var/www/blog.example.com/v1/project.php
To access project.php you go to blog.example.com/project, now I've also added a GET which is a title.
Instead of going to blog.example.com/project?title=This Title I want it to be something like blog.example.com/project/This Title
How may I accomplish that setup? Current .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^project/(.*)$ v1/project.php?title=$1 [L,QSA]
RewriteCond %{REQUEST_URI} !/(v1)/ [NC]
RewriteRule ^(.*)$ %{ENV:BASE}v1/$1 [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
Have it like this:
# turn off MultiViews to avoid conflicts
Options +FollowSymLinks -MultiViews
RewriteEngine On
# handle /project/abcd URI
RewriteRule ^project/([^/]+)/?$ v1/project.php?title=$1 [L,QSA,NC]
# add .php extension intrnally
RewriteCond %{DOCUMENT_ROOT}/v1/$1\.php -f
RewriteRule ^(.+?)/?$ v1/$1.php [L]
# forward everything to v1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!v1/)(.*)$ v1/$1 [L,NC]
Try it like this,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([\w-]+)$ v1/$1.php
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteCond %{REQUEST_FILENAME}.php !-d
RewriteRule ^project/([\w-]+)$ v1/project.php?title=$1 [QSA,L]

RewriteRule for Pretty URLs not working

The URLs of my site are of the type
http://localhost/cms2/pages.php?mpage=2
I have written the following .htaccess in order to create pretty URLs, but nothing happens
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)\/$ pages.php?spage=$1 [NC]
Mod-rewite is ennabled and other rewrite rules I've tested work. For example, I have a file called "contact-us.php". I can make it look "mysite.com/contact-us" using the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.*)\.html$ $1.php [nc]
Any help would be appreciated.
Thanks in advance.
Sonia
Not working because your regex is incorrect. Use this rule in your root .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([0-9]+)/([^./]+)/?$ /cms2/$2.php?spage=$1 [L,QSA]
This will rewrite a pretty URI: /3/page/ to /cms2/page.php?spage=3

Change url in htaccess didnt work

I have this url
mywebsite/chat.php?room=somenameHere
I want to change it like:
mywebsite/chat/somenameHere
I have tried that:
Options +SymLinksIfOwnerMatch -MultiViews
## Mod_rewrite in use.
RewriteEngine On
RewriteRule ^videos/([0-9]+)/?$ /details.php?object=1&id=$1 [L,QSA,NC]
RewriteRule ^articles/([0-9]+)/?$ /details.php?object=0&id=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^chat/([A-Za-z0-9-]+)/?$ chat.php?room=$1 [NC,L]
But nothing happen , it didnt work , is there something wrong ? i have looked many tutorials which all say same as i done.
Thanks for guidance!
EDIT:
im getting same page without changing anything and no error. getting this:
mywebsite/chat.php?room=somenameHere
You can use these rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /mainwebsite/
RewriteCond %{THE_REQUEST} /chat\.php\?room=([^\s&]+) [NC]
RewriteRule ^ chat/%1? [R=301,L,NE]
RewriteRule ^videos/([0-9]+)/?$ details.php?object=1&id=$1 [L,QSA,NC]
RewriteRule ^articles/([0-9]+)/?$ details.php?object=0&id=$1 [L,QSA,NC]
RewriteRule ^chat/([A-Za-z0-9-]+)/?$ chat.php?room=$1 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
EDIT: As per comments below:
RewriteCond %{THE_REQUEST} /(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]

Resources