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
Related
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?
I have a website that I'm trying to change the URLs on. All of the URLs start with http://domain.com/?
For example, http://domain.com/?index
I just want to remove the question mark. I don't care if it appears in the address bar, I just want my users to be able to access the pages on the site without having to type the question marks.
So, if a user wants to access http://domain.com/?index, I want them to be able to access it by typing http://domain.com/index.
Is this possible using .htaccess?
I've searched around and tried a few different things for a few days now and still can't figure out a way to accomplish what I'm trying to do.
Any help is appreciated.
Thanks.
Try:
RewriteEngine On
# Match against the request instead of the URI
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?([^&\ ]+)&?([^\ ]*)
RewriteRule ^$ /%1?%2 [L,R=301]
This takes URI's like http://example.com/?path/to/file.txt and redirects the browser to http://example.com/path/to/file.txt. The browser will display that URL in the location bar instead. This is, of course, assuming that if someone actually goes to that URL, that there is something there to be served other than a 404.
EDIT
To internally map none-query string URLs to the one with a query string:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /?$1 [L]
Try using :
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ /?$1 [NC]
in your .htaccess file ,
let me know 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.
Currently I'm appending the following url parameter:
www.somesite.com/?page_type=view
My php script uses this to determine which page view to load:
if (isset($_GET['page_type'])) {
$page_type = $_GET['page_type'];
$this->pageType($page_type);
} else {
$this->home();
}
I would like to be able use the following url to achieve the same thing:
www.somesite.com/view
So I need to redirect all requests to index.php while maintaining the original url input.
Then I can just use
$_SERVER['REQUEST_URI']
to get at the name of the page view.
Looking for some htaccess advice,
Thanks in advance!
You can use something along the lines of:
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>
This will redirect all requests that don't resolve to actual files/directories to your index.php. The ?url=$1 will contain the request but this may be optional in your case as you can still just get it from $_SERVER['REQUEST_URI'].
I left a RewriteBase line in there (commented out) as uncommenting can help with come server setups.
Another option you have is to simply set up your htaccess file to redirect any 404 (unresolvable) requests into the index file with a flag set like this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]
From here you can use PHP's access to the $_SERVER['REQUEST_URI'] information to parse things out in PHP and manually push variables into the $_REQUEST superglobal as needed to "map" your search engine friendly links into the application.
This gives you a lot more scalability than simply forcing the /pagetypevar format on all urls.
What happens later when you want to nest pages into subcategories? /info/about_us, /info/contact_us
What happens if you want to store other variables like items per page or pagination in the url? /products/1_My_supercool_item, /products/2_Another_item
Using a system that redirects all unresolved requests into the application framework, and allowing the application framework to do the remapping of urls will give you the most control and the most scalability.
Use a rewrite rule like this:
RewriteEngine On
RewriteRule /(home|view) index.php?page_type=$1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /^(.*)$ /index.php?page_type=$1 [L]
I have set up my .htaccess to redirect calls to mydomain.com/something.html to mydomain.com/index.php?q=something
This works fine.... but I noticed that since moving to a new hosting I am getting multiple queries for the query "missing".
I am pretty sure it is due to the default html page for wrong URLs being "missing.html" which is redirected as mydomain.com/index.php?q=missing. So any missing URL will cause my PHP script to be run with "missing" as input.
Is there a way to keep the URL redirect and manage the calls to missing.html without actually calling my PHP script for missing URLs?
Edit with solution; here is my .htaccess which does the redirect only if there is no existing URL:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)\.html index.php?q=$1
Seems the RewriteCond need to be placed before (as shown above).
You're looking for RewriteCond.
Try this in your .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
These rules will also stop rewriting for anything is an accessible path. If you want strictly missing.html, then the following rule will be sufficient:
RewriteCond %{REQUEST_URI} !/missing.html
Note these directives must come before RewriteRule.
Isn't is possible to add a rewriting rule which takes precedence over the other one and specifically rewrites mydomain.com/missing.html to the location of your 404 page? (Seing the redirection rule you already set up would help.)