I'm trying to write a simple blog which is working fine untill i get to the pretty links thing.
what i'm looking to do is use URLs like mysite.com/blog/this_post and pass that into the index.html file as a url parameter so index.html?blog=this_post
Been searching everywhere and found a bunch of htaccess code but most didn't work. I did find one solution that tries to work but for some reason isn't doing it correctly. (with and without the Options +FollowSymLinks part)
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^blog/(.*) /blog/index.html?blog=$1
when I use this i get /blog/index.html?blog=index.html in the url instead of keeping the /blog/this_post and porting that into the index.html as expected. Could something on the server be set incorrectly? everything else with the blog is working like a charm but so nothing wrong with the script its just getting the pretty links to work.
Looked at an older site that has wordpress and coppied over that htaccess code but it does that same thing. All the other searches on here and other sites point to the same solution above or the wordpress but for some reason it's passing index.html to the script and not the last url segment "this_post" as expected.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.html [L]
</IfModule>
# END WordPress
what should happen is the user types in
mysite.com/blog/this_post
which remains in the address bar correct? but the htaccess file pulls this_post and passes it as an argument to the index.html script. which is what the examples above should do. why it's changing the address bar to
mysite.com/blog/index.html?blog=index.html
i'm not sure why
side note, since i got this before...yes i'm aware of using the .php extention but .html is a client ask that they're not budging on.
edit-the link for possible duplicate was one I already found and tried to get the current RewriteRule but it's still not working.
The best way is to made something simple like this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([^/]*)\.html$ index.php?blog=$1 [L]
Options -MultiViews
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
then you can do URL like:
mysite.com/this_post
what is equal to:
mysite.com/index.php?blog=this_post
Like that you can do both calls and will work fine.
Generaly you need first decide how you want your SEO URL to look like and then you need to setup .htaccess regex and rules.
NOTE: you can't use .html for GET calls. You can use .php files for your works and use $_GET['blog'] to pickup your data from URL.
Seem to have found the combo that works, the [NC, L] flag combo keeps giving me a crash error so i left it just [NC]. Having it in the root account directory seems to not work also so i moved into the web directory (public_html) and that works for now.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/(.*) /blog/index.html?blog=$1 [NC]
Still not sure why the L flag gives me a server crash on the pages or why having it in the root doesn't work but moving it up into the public_html folder does, is it possibly a server configuration problem that i'm overlooking?
Related
I'm currently writing a PHP application, and I have always used frameworks and always worked in larger projects, but for this particular project I am doing neither. I've hit an issue where if I go to:
http://localhost/project
It runs the index.php file fine, but if I wanted to go to
http://localhost/project/test
I'd still want it to run the index.php file, and work out what to do with the 'test' part from there. Instead I'm getting a 404. How do I go about this?
I'm using Apache as my server, is it something I need to change in the .htaccess file? I'm pretty new to editing that file and have found most tutorials haven't said how to do this, but it seems like a pretty integral thing
edit:
This is what my htaccess currently looks like
Options +FollowSymLinks
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
Try to change your .htaccess file into :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)(.*)/?$ index.php
as initial solution to check if it works
Okay I'm trying to use Lando (landocms.com) and I'm trying to get the pretty urls option to work.
Basically by default Lando creates link like: domain.com/index.php/page. Supposedly, there is a way to remove the index.php so the links become: domain.com/page. I have created an .htaccess as directed, however it does not work.
Here is the .htaccess I am using:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I have tried alot of variations, /index.php/, index.php? and plenty more but none work. According to HostGator everything should be fine. Any thoughts? I think I'm going crazy haha.
Thanks!
Rewriting for a CMS is a two-tier approach. First, you need to set your .htaccess (I have put a safer one here for you):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ index.php [QSA,L]
Then, LandoCMS allows you to remove the index.php from the generated addresses, by means of turning on the appropriate setting in the administration panel. See this link for more information.
If the .htaccess content I've given you doesn't work, then simply use the one that the CMS has given you.
You want to remove the index.php part from any URL, but process the incoming, friendly URLs through index.php nevertheless
RewriteEngine On
# remove index.php and redirect client
RewriteCond %{ENV:REDIRECT_SEO} ^$
RewriteRule ^/?index.php/(.*) /$1 [R,L]
# process friendly URL
RewriteCond %{REQUEST_URI} !^/index.php/
RewriteRule .+ /index.php/$0 [E=SEO:1,L]
The environment setting E=SEO:1 prevents an endless loop.
I am having a heck of a time trying to get an apache mod_rewrite rule to work. The thing that is making the process the most difficult is that I have no way of knowing what the final output string is. I continue to get 404 pages, but that doesn't tell me much except that I did something wrong. Is there some method of echoing out the final rewrite rather than redirecting me to a 404?
. . . And just in case someone feels like helping me out with my original problem, I'm trying to make it so that anyone who goes to /server/root/to/folder/public_html/ gets redirected to /server/root/to/folder/public_html/destinationFolder/. Below is one of the hundreds of variations that I've tried:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(favicon\.ico|assets)
RewriteRule ^public_html/(.*) destinationFolder/$1 [L]
</IfModule>
You are using system paths. Apache mod_rewrite only works with URLs (PT flag is implied in .htaccess files), that's your first error.
public_html is normally the root directory. For example: http://mydomain.com
Then, a complete rule set could be something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(favicon\.ico|assets) [NC]
RewriteRule ^(.*)/?$ DestinationFolder/$1 [L]
Maps silently
http://mydomain.com/Anything
To:
http://mydomain.com/DestinationFolder/Anything
Except when anything is file favicon.ico or folder /assets
For permanent redirection, replace [L] with [R=301,L]
This is just an example. The rules and parameters have to be modified to meet the real requirements.
I am using kohana 3.2
My site is www.mysite.com/best, kohana is installed on best.
I am using the default htaccess file with rewritebase /best
so what is the best way to redirect users that go to www.mysite.com?
Right now if someone puts www.mysite.com/helo (www.mysite.com/best/helo)
It loads but apache gives 404 not found.
I hope my question makes sense.
EDIT
If a user goes to main domain (www.mysite.com) I want it to load kohana located at mysite/best.
but if a user right now types the full thing and omits best, site will load but my forms wont work because it is posting to www.mysite.com/helo not www.mysite.com/best/helo
instead of loading the fake helo controller, it must show a page not found.
EDIT
Ok I added this to htaccess file and it works perfectly
RewriteCond %{REQUEST_URI} !^/best/
RewriteRule ^(.*)$ /best/$1
You can't have multiple rewritebase in htaccess so...
rewrite your rules to fit you need in /best
or put an other htaccess in / to rewrite your needs.
Try this .htaccess instead:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /best/
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
#RewriteRule .* index.php?kohana_uri=$0 [PT,L,QSA]
RewriteRule .* index.php?/$0 [PT,L,QSA]
Note that the trailing / on RewriteBase is required.
I've been working on this for a while and have tried a lot of different solutions I've seen on the web and can't seem to get this to work.
I have a site at www.mydomainname.com. The page that I want to handle ALL page requests is www.mydomain.com/index.php. I'd also like to set this up to work for any other domains that I point to this code base (using wildcards would be the way to go for that I think).
So the following URL types (or any other) should automatically go to index.php, while still keeping the original URL structure in the browser address bar:
www.mydomain.com/
mydomain.com/
www.mydomain.com/item/111
www.mydomain.com/item/itemname/anothervariable/value
www.mydomain.com/item/itemname/?variable=value
I'm using PHP 5 and a recent version of Apache with mod_rewrite enabled.
Thanks in advance for any help!
Simple:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !\.(jpg|gif|ico|png|bmp|css|js)$
RewriteRule .* index.php
You could use the follow RewriteRule
RewriteEngine On
RewriteRule ^(.*)$ /index.php?originalUrl=$1
Untested, but it should work. You will then also have the original URL available in the 'originalUrl' GET parameter for further parsing.
Include this once per .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule (.*) index.php
If you need the information from the matched URL you can modify your RewriteRule to match portions of the old URL or just include everything by using the variables $1 and so forth. If for instance you wanted to get the item number passed in quickly to index.php, you could use this rule:
RewriteRule item/(.*)$ index.php?item=$1