I read lots of tutorials and articles about Friendly URLs through htaccess file, but probably I am missing something.. Thus, I have some questions in order to understand..if someone can help me to clear these out, please!
First of all, before adding any rule to htaccess file,
1. Should I have links like article.php?article_id=1 or the friendly urls I want inside into my files, like: 2016/05/article_name ???
2. I found I link here through another post, that gives a web tool in order to give the rule of the htaccess file: http://www.generateit.net/mod-rewrite/index.php
So, I placed my actual 'ugly' link and gave me right rule to write in htaccess file.
Now, I have uploaded the htaccess file and I am navigating to my website.. If I have the 'ugly' links I can see everything correctly! While I am pressing the friendly url manually there was to issues:
First, I could not see the website's css and javascripts files.. I put the absolute path according to an article, so I guess I am fine with that (is that correct?).
Second, If I press a link let's say the logo, in order to go back to home page, it keeps the rule of htaccess and navigate me to "not found page".. cause the link is something like that: mywebsite.gr/2016/05/index.php, but year and month does not actually exists ...
I would like to have a rule in order to keep the same articles links (I do not have problems about the other links, categories, menus and so on.. just the links of articles). The link I have now is (after all I see and understood):
article.php?article_id=1&year=2016&month=05&name=test-1
and want to have:
mysite.gr/2016/05/test-1.html
*I guess I should mention that the Domain NS does not change yet, and I am working through the temporary link that host provides me.. something like: linux.41.24.23.4.server.gr ... and so on
thanks a lot in advance!! (and sorry for the possible duplicate..)
I think I found a solution... not sure if it is the best one..
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]{4})/([0-9]{2})/([a-z0-9\-]+)\/?.html$ category.php?file=$1&year=$2&month=$3&art_name=$4 [NC,L]
</IfModule>
in order to show my links this way:
mydomain.com.gr/2016/06/this-is-an-article.html
of course inside my website, I slit the article name coming from my DB, in order to add the middle slashes and replace any other symbol with 'nothing':
$art_name = "this is an article!";
$art_name = str_replace('!','',$art_name);
$art_name = str_replace(',','',$art_name);
$art_name = str_replace(' ','-',$art_name);
The final link inside my website is:
click me
Furthermore, about the other links: CSS - Js - a tags and so on, as long as I understood there are some options in order to work properly:
You could use Absolute path, or start your links like this (with forward slash in the beginning):
<link href="**/**main.css" rel="stylesheet" type="text/css" />
it's working like an absolute path I believe, cause the file is seeing the root folder (for example for me is localhost - I am working locally with xampp)
I believe I helped someone who have the same questions..
Related
I have a web page which works fine on live server. However some links to files (jpg, pdf and others) which are created with cms editor contain relative paths.
When I run that page on my local test server which serves the pages out of a sub folder of localhost the relative paths to the files are wrong since they are missing the subfolder at the beginning. The html page loads fine. It's just some files in it that have wrong path and won't load.
page loads from http://localhost/level1/
files are trying to load from http://localhost/level2/ and I get 404s.
They should be loading from http://localhost/level1/level2/
So I setup a RewriteRule to correct the path but no matter what I have tried I can't get it to work. I have tried various flags including [R,L] but nothing changes the URI in the html.
currently I have:
RewriteRule ^/level2/(.*)$ /level1/level2/$1 [R]
Any suggestions?
Thanks
Sounds like those links are not relative paths but absolute ones (starting with a leading slash (/). That is why the issue occurs at all. Relative paths make much more sense.
This would be the version to be used inside your http servers host configuration:
RewriteEngine on
RewriteRule ^/level2/(.*)$ /level1/level2/$1 [L,QSA]
Here the version for .htaccess style files (note the missing leading slash):
RewriteEngine on
RewriteRule ^level2/(.*)$ /level1/level2/$1 [L,QSA]
You could use a version that can be used in both situations:
RewriteEngine on
RewriteRule ^/?level2/(.*)$ /level1/level2/$1 [L,QSA]
Note however that in general one should always prefer to place such rules inside the http servers host configurations. .htaccess style files are notoriously error prone, hard to debug and they really slow down the server, often for nothing. .htaccess style files only offer a last option for those who are using a really cheap web hosting provider. Or for situations where a web application has to write its own rewrite rules, which obviously is a security nightmare on its own...
This is a link to a website I am making (I am very new to this) - http://www.inquivesta.iiserkol.ac.in. I am using .htaccess to try and rewrite the URLs for some of the pages. One of them is the about us page whose actual link is - www.inquivesta.iiserkol.ac.in/Home/index.php and I want it to look like www.inquivesta.iiserkol.ac.in/about/
So I wrote this in the .htaccess file :-
RewriteEngine On
RewriteRule ^about/?$ /Home/index.php [NC,L]
This doesn't seem to work. Whenever people open this page, I don't want it to display the filename. But the filename is stil being displayed. And if I try to open http://www.inquivesta.iiserkol.ac.in/about/ then it redirects to www.inquivesta.iiserkol.ac.in/Home/index.html
I've searched and tried many of the examples listed on this site, but none seems to work right for me.
We have a running forum in /forum directory, which was previously named /vanilla2. Moving the forum (today) went ok and all internal links work fine. Redirecting the root of previous forum to the new one works ok using this:
RedirectMatch 301 /vanilla2/.* http://www.example.com/forum/
However the problem is because there are many posted links throughout the forum which still point to the old directory and its subdirectories, for example:
http://www.example.com/vanilla2/discussion/54684/blah-blah, which should now be:
http://www.example.com/forum/discussion/54684/blah-blah
Here's my directory structure:
/root
.htaccess (for WordPress)
/forum
.htaccess (for forum)
/wp
/other_dirs and its subdirectories
My knowledge is obviously quite limited so I need to know what exactly to put in which of existing two htaccess files?
Thank you.
Put this code as first rule in your DOCUMENT_ROOT/.htaccess file ( a level above /forum/):
RewriteEngine On
RewriteRule ^vanilla2(/.*)?$ /forum$1 [L,NC,R=301]
I have very similar problem like this one: Apache mod_rewrite - prefer files over directories with pretty URLs
However it is not same, and solutions mentioned in above link doesn't work for me.
My directory structure looks like this:
/pages/articles/january.php
/pages/articles.php
/pages/home.php
/articles/
/index.php
Now, I am including in index.php pages (depending on url).
For example, when user types address www.domain.com (or www.domain.com/home), index.php will include /pages/home.php
But if I enter this URL: www.domain.com/articles it will make link something like this: www.domain.com/articles/?page[]=articles (in other words index.php won't include /pages/articles.php file)
On the other hand, this works perfectly: www.domain.com/articles/january
This is my htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9+]*)/([a-zA-Z0-9+]*)$ %{DOCUMENT_ROOT}/index.php?page[]=$1&page[]=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9+]*)$ %{DOCUMENT_ROOT}/index.php?page[]=$1 [QSA,L]
I use array for page because I can have subpages (and subpages work fine!).
EDIT: I have found this however it doesn't solves my problem :(
Can someone tell me why it does this and how to fix it?
Or, how can I give priority to files instead directories?
EDIT 2: I solved it by removing "/articles/" directory, however I am still interested how to make it work through htaccess file rules.
Per the comments above, your original issue was worked around by removing the articles directory, but you still wanted to be able to deal with directories in that sort of situation.
You'd probably want to split into two sets of rules. Have an earlier rule that uses the -d flag in a RewriteCond to catch directories so that it could treat them differently as needed. Alternately, ignore directories in the first rule by negating the flag (!-d) then catch them in the later rule.
I hope I can explain this clearly enough, but if not let me know and I'll try to clarify.
I'm currently developing a site using ColdFusion and have a mod_rewrite rule in place to make it look like the site is using PHP. Any requests for index.php get processed by index.cfm (the rule maps *.php to *.cfm).
This works great - so far, so good. The problem is that I want to return a 404 status code if index.cfm (or any ColdFusion page) is requested directly.
If I try to block access to *.cfm files using mod_rewrite it also returns a 404 for requests to *.php.
I figure I might have to change my Apache config rather than use .htaccess
You can use the S flag to skip the 404 rule, like this:
RewriteEngine on
# Do not separate these two rules so long as the first has S=1
RewriteRule (.*)\.php$ $1.cfm [S=1]
RewriteRule \.cfm$ - [R=404]
If you are also using the Alias option then you should also add the PT flag. See the mod_rewrite documentation for details.
Post the rules you already have as a starting point so people don't have to recreate it to help you.
I would suggest testing [L] on the rule that maps .php to .cfm files as the first thing to try.
You have to use two distinct groups of rewrite rules, one for .php, the other for .chm and make them mutually exclusives with RewriteCond %{REQUEST_FILENAME}. And make use of the flag [L] as suggested by jj33.
You can keep your rules in .htaccess.