Redirect to other location - .htaccess

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]

Related

How to let htaccess distinguish between files and ids

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d # not an existing dir
RewriteCond %{REQUEST_FILENAME} !-f # not an existing file
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f # and page.php exists
# redirect to the physical page
RewriteRule ^(.*)$ $1.php [L]
# otherwise, redirect to serve.php
RewriteRule ^ /serve.php [L]
RewriteRule ^(\w+)$ ./serve.php?id=$1
So in the first part of the code I just turn http into https. My aim is that I can use urls without an .php but in my old code I had the problem that if I did so, it was used as an id for my serve.php page. So if use https://example.com/contact it was like https://example.com/serve.php?id=contact but I want it to work as https://example.com/contact.php but on the other side I want that ids that arent directions or file should still work as ids. My old code was...
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(\w+)$ ./serve.php?id=$1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You can use these rules in your site root .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# skip rules below this for files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# rewrite to the physical php page
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# otherwise, redirect to serve.php
RewriteRule ^/?$ serve.php [L]
RewriteRule ^(\w+)/?$ serve.php?id=$1 [L,QSA]

.htaccess /subdirectory/ -> /subdirectory/index

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]

Set language using htaccess URLrewrite

I have for example two files in my root directory: one.php and two.php. I would like to reach them through these urls without having to actually have the physical directories en+de.
mydomain.com/en/one
mydomain.com/en/two
mydomain.com/de/one
mydomain.com/de/two
So the first directory would be "ignored" and just pass a GET variable with the language setting to the php-file, second directory is the php-file without extension.
This is what I'm using in my htaccess to loose the file extension so far:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
UPDATE/SOLUTION
This is quite useful: .htaccess rule for language detection
and this seems to pretty much do what I wanted:
RewriteRule ^(en|de|fr)/(.*)$ $2?lang=$1 [L,QSA]
RewriteRule ^(.*)$ $1?lang=en [L,QSA]
ONE MORE ADDITIONAL QUESTION
For mydomain.com/en/one/id45
If I wanna pass id45 as a variable to one.php what line in htaccess do I have to add?
You can try this rule:
DirectoryIndex index.php index.html
DirectorySlash On
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$2.php -f
RewriteRule ^(en|de|fr)/([^/]+)(?:/(.*)|)$ $2.php?lang=$1&var=$3 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/?$ index.php?lang=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ $1?lang=en [L,QSA]

Hide directory name from URL

I just want to hide the dir name from URL.
From: example.com/dirname/somepage
To: example.com/somepage
That code doesn't work for me, I have probably made some mistakes
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule / /dir/$1 [L]
I have already this in .htaccess (to hide php extension)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
This should be your complete .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+dirname/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^dirname/)^(.*)$ /dirname/$1 [L,NC]

htaccess works but content doesn't load

When I use index.php?id=this-is-an-article the page loads with the content, when I use /articles/this-is-an-article the page loads without any content, any idea as to how I can resolve this?
.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# force www. in all requests
RewriteCond %{HTTP_HOST} ^mysite\.net [NC]
RewriteRule ^(.*)$ http://www.mysite.net/$1 [L,R=301]
# enable hiding php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^(.*)\$ $1.php
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^page/(\d+)*$ ./index.php?p=$1
# index.php?id=this-is-an-article => /articles/this-is-an-article
RewriteRule /articles/(.*) index.php?id=$1 [NC,L]
Thanks in advance.
Get rid of the leading slash. It's also best practice to begin a regex like that with ^ and end it with $.
RewriteRule ^articles/(.*)$ index.php?id=$1 [NC,L]

Resources