.htacces Nice urls for subfolder - .htaccess

I have 1 subfolder located at my root folder:
/root/task6/index.php
And I need to change urls www.andriussulcas.prinusprojects.lt/task6/index.php?p=naujienos&title=title to www.andriussulcas.prinusprojects.lt/task6/naujienos/title
I've been trying to do this for some time now and all my efforts weren't successful.
While searching for answer, I started thinking that maybe my mod_rewrite wasn't activated. But later on I found how to redirect my web site root to subdirectory, where all my files are. So mod_rewrite works.
I found some similar answers here, on stackoverflow, like how to "rewrite rule" for a sub-sub-directory? .htaccess/php but for some reason it doens't work for me.
Here's my .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?andriussulcas.akademija.prinusprojects.lt$
RewriteCond %{REQUEST_URI} !^/task6/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /task6/$1
RewriteCond %{HTTP_HOST} ^(www.)?andriussulcas.akademija.prinusprojects.lt$
RewriteRule ^(/)?$ task6/index.php?p=naujienos [L]
RewriteRule ^naujienos/([A-Za-z0-9-]+)?$ index.php?p=naujienos&title=$1 [L]
.htaccess file is located at my root directory, and there are no other .htaccess files at subdirectories.
All help is much appreciated.

Try replacing your last rule with this:
RewriteCond %{HTTP_HOST} ^(www.)?andriussulcas.akademija.prinusprojects.lt$
RewriteRule ^naujienos/([A-Za-z0-9-]+)$ /task6/index.php?p=naujienos&title=$1 [L]

Related

Point to a file in subfolder with .htaccess

I've wanted to move my index.php file from the root to /system/ folder, this is for no reason but giving myself opporunity to learn more about .htaccess, which I am finding very confusing.
How can I achieve this? Currently my code looks as follows
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?query=$1
This allows for example mydomain.com/some-url/ to become /index.php?query=some-url for example, this far I'm with it all. But moving the file into System folder and addind /system/ before /index.php does nothing. How does one do this? Thanks!
This could be done with a simple rewriterule. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^(.*)$ system/index.php [L]

htaccess redirect root to subfolder - but keep other root subfolders beyond

My server has several system folders and files by default. so for better overview I want to put my website in a subfolder like this:
www.domain.de/web/index.html
ALL links and website-related stuff is found in this subfolder "web". BUT the folder "web" should not be seen in the url!
I got this with these lines in my htaccess and it works:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.de [NC]
RewriteCond %{REQUEST_URI} !^/web/.*$
RewriteRule ^(.*)$ /web/$1
But now the problem:
I also want other folder on my server to work. So www.domain.de/downloads/ should work, too. This folder is NOT in the subfolder "web". So if I write the direct url "www.domain.de/downloads" in my webbrowser, I don't want to get a redirect to "www.domain.de/web/downloads/", you know?
Is there any solution? :)
Thanks!
You Can use a RewriteCond to exclude your folder :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.de [NC]
RewriteCond %{REQUEST_URI} !^/download [NC]
RewriteCond %{REQUEST_URI} !^/web/.*$
RewriteRule ^(.*)$ /web/$1

What all do I need in my .htaccess, such as permissions, etc

I've got a .htaccess file at the root directory of my website, and I've made sure .htaccess is the extension, not .htaccess.txt. I'm trying to clean up my .php urls, and even after looking at all the other questions about this, it is not working.
I'm quite strongly sure that the code for removing the extension is correct, so it makes me wonder if there is other code that I need inside the .htaccess for permissions or something like that. Thanks for any answers, and please don't mark this as duplicate... I've searched for hours on Stack Overflow, SitePoint, and all the other websites.
Here is the only code inside my .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1\.php
Just to make sure it's clear, I want the urls:
website.com/about.php
website.com/home.php
website.com/blog.php
to look like
website.com/about
website.com/home
website.com/blog
It should remove .php from ALL .php files in all directories for my website.
you actually want
RewriteEngine On
#if the requested file doesn't exist on server filesystem
RewriteCond %{REQUEST_FILENAME} !-f
#AND if the file with php extension exists. Just an extra check
RewriteCond %{REQUEST_FILENAME}\.php -f
#AND if the request isn't for domain root
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1\.php?r=1 [L]
#redirect all urls ending with .php to "clean" URL
RewriteCond %{REQUEST_URI} ^/?(.*)\.php$
RewriteCond %{QUERY_STRING} !(^|&)r=1
RewriteRule ^(.*)\.php$ /$1? [L,R=301]
It will not make website.com/about.php look like website.com/about but it will display website.com/about.php if user visits website.com/about.

htaccess and rewriting urls

currently i have a /en/ folder that is empty except for a .htaccess with the following
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ ../index.php?language=en$1 [NC]
i use it to eliminate the need for
index.php?language=en
in all my URLs. I would like to modify the htaccess in a way that i no longer need the /en/ folder with nothing but the htaccess inside. ideally i would like an htaccess in my root folder that reads the url and if it is www.example.com/en/ to rewrite to www.example.com/index.php?language=en
This should work for you:
RewriteRule ^en/(.*)$ index.php?language=en$1 [NC]
Put the following code in .htaccess file in your root folder.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^en/(.*)$ index.php?language=en$1 [L]
Here $1 will append rest of the url as well. The condition will also help if you request your files using direct url.

Htaccess rules to rewrite pages inside folder to the root?

The is my real folder scheme:
ROOT
index.html
news.html
+articles
|-obama.html
|-oil.html
I want some htaccess rule so if people go to domain.com/obama.html the server will fetch the one in the articles folder without redirecting.
If some one goes to domain.com/index.html will still fetchs the one in the articles even if there is an index in the ROOT.
Thanks
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond articles/%{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ articles/$1 [QSA]
RewriteRule .* articles/$1 [L,QSA]
Why don't you just use:
# RewriteRule /obama.html$ /articles/obama.html [L]
RewriteCond %{REQUEST_URI} !^/{index,news}.html$
RewriteRule (.*)$ /articles/$1 [L]
?

Resources