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]
Related
I've taken over a former site/domain, and set up a new site using Wordpress. The WP installation rewrites URL's to static ones, as you'd expect it to.
At the same time I want to preserve the former pages, as they have incoming links. I'm not interested in 301'ing them to "new" pages.
The old URL structure is /index.php?id=123, which I suspect is causing the problem with the WP .htaccess file. For reference, this is what it looks like:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I've tried adding the following:
RewriteRule ^([0-9]+).html index.php?id=$1 [R,L]
Doesn't work. Just redirects to site.com/?id=123 and shows the front page.
I should add that I plan on just adding these new pages as regular static HTML files in the format of 123.html, 321.html etc.
How do I use .htaccess to make this work together with the WP installation and what WP puts into the .htaccess file?
To clarify:
I want to have my 123.html static HTML page be index.php?id=123. When you access index.php?id=123 it should bring up 123.html, but show index.php?id=123 in the address bar. If you access 123.html it should 301 to index.php?id=123.
To map an URL with a querystring up to an actual file you'll need to use a RewriteCond to match the querystring itself (as RewriteRule doesn't):
Something along these lines ought to do it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# retrieve X.html when index.php?id=X is requested
RewriteCond %{REQUEST_FILENAME} index\.php
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteCond %1.html -F
RewriteRule .* %1.html? [L]
# standard WordPress routing
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This will first check to see if you've got a request for index.php with a querystring like id=X.
Then it'll check to see if a file called X.html actually exists; I'm not 100% happy about having to use the more system hungry subrequest file check -F rather than the standard -f but I can't see a way around it in .htaccess in this case.
If X.html actually exists, it'll fetch that file whilst leaving the URL as index.php?id=X.
However if that file doesn't exist it'll fall back to standard WordPress no file, no directory routing to index.php
I'm not a WordPress expert but that should work; I guess the main WordPress controller uses $_SERVER['REQUEST_URI'] to determine the action.
Note: This won't, however, prevent people from accessing 123.html directly by going to the URL www.site.com/123.html - I kept falling into infinite loops and Apache 500 errors trying to prevent that :|
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.)
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
Hai
I have worked with clean URL in php. Now I want to convert a clean URL to normal php URL Like
http://localhost/url/user/2/a to
http://localhost/url/user.php?id=2&sort=a
Can any one give me the way to do this?
also i have one more question. Is there is any way to do this with out .htaccess?
In your .htaccess file in your root directory:
RewriteEngine On
RewriteRule ^url/user/(\d+)/([a-zA-Z]?)$ /url/user.php?id=$1&sort=$2
should do it.
I'd suggest not to write specific rule for the every module, but make a front controller which will receive all requests and the dispatch them to the corresponding modules.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?uri=$1 [QSA,L]
So, you'll end up with $_GET['uri'] parameter in your script, which can be parsed to get required values
i have urls like
http://mysite.com/index.php?p=resources
http://mysite.com/index.php?p=resources&s=view&id=938
but i want urls like
http://mysite.com/resources
http://mysite.com/resources/view/938
instead of making hundreds of rewrite rules i wonder if it would be possible to just have one? Ive head this is possible by "getting the uri and splitting it into parts" and then just add a rewrite rule for index.php
but how? could someone give an example or link a tutorial
I happen to use this in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
This essentially calls index.php no matter what is requested. Then inside your PHP code you can look at $_SERVER['REQUEST_URI'] to get the URL and parse it accordingly.
The RewriteCond lines are there to exclude direct calls to files. In my case I don't want stuff like requests for js/css/image files to go through index.php for performance reasons.
Create a .htaccess file. Not somefilename.htaccess, it is simply named .htaccess.
Note: Your index.php and .htaccess file should be in the same directory.
Now try this on your .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9\-]+)$ index.php?p=resources
RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/([0-9]+)$ index.php?p=resources&s=view&id=938
</IfModule>
see more about url rewrite here