How to fix double href in .htaccess? - .htaccess

Following a tutorial I set up routes for a webapp written in PHP. Everything works until certain point. For instance, when I click a link from the base it takes me to the desired page e.g. about/culture. But when inside culture page I got about/about/culture or about/about links what is wrong because these routes don't exist. I pretty new on this, How I can fix it up?
My .htaccess file is this:
RewriteEngine On
RewriteBase /PHP/20-DI_Container/
RewriteCond %{REQUEST_FILENAME}% !-d
RewriteCond %{REQUEST_FILENAME}% !-f
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteRule ^(.*)$ index.php?route=$1 [QSA,L]

Related

htaccess rewrite for routing from a sub directory

I've been using this .htaccess file for enabling the grav Cms for a few sites:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/grav/
RewriteRule ^$ /grav [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /grav/$1 [L,QSA]
This works flawlessly as long the site is at the document root.
Now, I have a new site that starts in 2020/ and has Grav installed in 2020/grav.
I modified the above .htaccess to be:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/2020/grav/
RewriteRule ^2020$ /2020/grav [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^2020/(.*)$ /2020/grav/$1 [L,QSA]
This .htaccess file is located in /2020/.
While in the first case the url is left as is and Grav gets the rest of the url as parameters, in the second case (2020/) the url changes to /2020/grav, which is not what I want?
I wanted to check if it was a Grav issue and I've setup a simple test case, with the above .htaccess file and a grav/ directory with only an index.php file in there and I got the same result.
Any hint on how I have to modify the .htaccess file to get 2020/abc to stay as is in the url bar and at the same time have Grav to get the abc argument?
You can have this code in 2020/.htaccess:
RewriteEngine On
RewriteRule ^$ grav/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!grave/)(.+)$ grav/$1 [L,NC]
(?!grave/) is negative lookahead condition that will skip match if match already starts with grave/
The original question was about getting it to work with an .htaccess.
#anubhava gave a very good answer for the general case. But it sadly was not enough for Grav.
I got some more help in the Grav forums and at the end of a long journey Ole found out that the question has been asked in their forums a long time ago and a solution was found!
The /2020/.haccess can be simply defined as:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/2020/grav/
RewriteRule ^(.*)$ /2020/grav/$1
But what one need is to add a couple of configurations in the /2020/grav/user/config/system.yaml file:
custom_base_url: 'http://domain.tld/2020'
session:
path: /2020
You can find a more detailed question and Ole's reply in the Grav forums

Syntax with .htaccess URL rewrite

I need to get the following URL rewrite syntax correct and am struggling.
xxxxx.com/public_html/food/
Needs to be rewritten as:
xxxxx.com/food/
I tried this and it doesn't work:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/public_html/food/(.*)$
RewriteRule ^(.*)$ http://xxxxx.com/food/%1 [R=301,L]
My client is using Joomla (which I am not familiar with), so I need to do so using the .htaccess file per everything I have researched so far. I am just struggling getting the syntax to work correctly though.
Thanks!
Try this, .htaccess
for Rewrite
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [QSA,L]
for Redirect (add R=301)
RewriteEngine On
RewriteRule ^food/?(.*)$ http://xxxxx.com/public_html/food/$1 [R=301,QSA,L]
Edit:
If you want to rewrite all urls (including /food)
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public_html/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public_html/$1 [QSA,L]
It will allow you to access site like,
http://xxxxx.com/food/ will point to http://xxxxx.com/public_html/food/
http://xxxxx.com/sample/ will point to http://xxxxx.com/public_html/sample/
http://xxxxx.com/anything/cool/?product=123 will point to http://xxxxx.com/public_html/anything/cool/?product=123/

htaccess, rewrite multiple pages to the same page but keep original value somewhere

I have been fiddle farting around with htaccess and RewriteEngine but I can't quite get my head around it...
I'm building a website on which I want users to be able to go to /portfolio/typography for example. But I don't want to create seperate pages for each category in this portfolio and thus I want to rewrite (redirect?) all the requests that go to /portfolio/ to the index.php of this directory and load the appropiate projects for this category from there.
Any ideas on how I could do this? I used this to redirect all the requests to /portfolio/:
RewriteCond %{REQUEST_URI} !/portfolio/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /portfolio/ [R=302,L]
Thanks in advance,
Cas Cornelissen
EDIT
Maybe I should note that I have another .htaccess file in the root of my website.
Ok, so I found the answer to my own question...
Seems like the following is working:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]

Limit RewriteRule in htaccess file

i got a site php based with some rules in the .htaccess file to get rid of file extentions in the url adress bar. Basically it takes http://netbureau.com.br/en/about.php/ and turns it into http://netbureau.com.br/en/about.
Here are the lines in the htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The problem comes when i try to access the rss feed of the blog at http://netbureau.com.br/blog/?feed=rss2 and when i try to set custom permalinks for the blog at http://netbureau.com.br/blog. It gets messed up by the htaccess file.
So is there any way to disallow the RewriteRule for the /blog folder so that i can get back my rss link and set custom permalinks in the blog?
I know it's at the same time Wordpress related but it feels more connected to the htaccess file than Wp.
Thanks in advance.
EDIT #1:
I've set up the wordpress permalinks to the default structure which goes like this: http://netbureau.com.br/blog/?p=123 This made my rss link back for good.
The remaining problem is that Wordpress gives me its own rewriterule which is:
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
Is there a way to to still use the first rule to apply to the whole site except the /blo/ folder and apply the WP rule only to the /blog/ folder?
I've tried different combinations but without luck so far. I could only have the site without the custom links for the blog or the custom blog links and a 404 on the pages of the site.
Try:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/blog/?
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The !^/blog/? expression excludes any URI that starts with /blog

Do I need to call for .htaccess?

I have no experience with .htaccess, but I got a tip that it's very useful so I wanted to try this.
I now have a file called .htaccess, in my root folder.
The files contains this;
RewriteBase /
RewriteEngine on
RewriteCond %{HTTP_HOST} ^kellyvuijst\.nl [nc]
RewriteRule (.*) http://www.kellyvuijst.nl/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
What I'm trying to do here is create a 'www.mysite.com/portfolio/' instead of 'mysite.com/portfolio.html' I used some tutorials on this and I think it's correct, but I'm not sure.
So now I have this file, and what now? The tutorials all show what to put in the file but not what to do with it? Do I need to call for it in every .html page I have? And how do I call for it?
A .htaccess file is automatically invoked by the server.
You have just to put this into your file :
RewriteBase /
RewriteEngine on
RewriteRule www.mysite.com/portfolio/ /mysite.com/portfolio.html [L]
Hmm, you're using a lot of rules here to achieve just that.
Anyway, no you don't have to include that file. If you're hosting your site on a server with Apache it'll be included automatically. Can you also run PHP files or is your site just HTML? That's always an easy sign if you're also using Apache (not 100%, but often the go together).
If so, you could try just using these rules first:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.+)\.(.+)$ [nc]
RewriteRule ^(.*)$ http://www.%1.%2/$1 [R=301,L]
If that always adds www to your address, even if you type in the URL without www at least you can be certain that it works.
Then, to make the .html disappear you can add this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule $(.*)/$ /$1.html [L]
This should make every url that ends with a slash (like portfolio/) use a .html file instead (portfolio.html), but only if /portfolio/ isn't an actual directory on your website.
(I removed your url from the rules because this way it should also work if you use it on another website, or if you change your url. It should still do what you want)
Made sure the server is configured to allow htaccess files to override host options. So in your vhost/server config, you need:
AllowOverride All

Resources