Rewrite Rule Issues - php

I hate to write this post but i've been working on this for far too long.
We are upgrading a multilingual site and the language module that we are now using handles URL's very differently. We are moving from domain.com/location-fr-fr/ to domain.com/fr_FR/location/. I have been able successfully handle rewriting that by using the following RewriteRule (.*)-fr-fr fr_FR/$1/. I cannot figure out the english one which looks like this. domain.com/location/ has to go to domain.com/en_US/location/
Where im running into the issue is I can't just rewrite everything, as I need to exclude any url beginning with /en_US or /fr_FR, but im failing with that.
I have tried this which i really think should work
RewriteRule ^(?!en_US/) http://www.google.com
I tried adding in a preceding slash but no good either
RewriteRule ^(?!/en_US/) http://www.google.com
https://htaccess.madewithlove.be/ agrees with me that it should work too but in practice it does not. I have been doing all my testing using wget so that browser caching is not an issue.
Can someone give me a couple of pointers? I'm open to any suggestions on how to make this work better.
It's a Silverstripe website in case that affects rewrites too.
Edit:
After removing Silverstripe rewrite rules the rule works fine.

Related

All URLs are redirected to index.php

I have setup a Symphony framework on my localhost using this tutorial. I am using the PHP default server and MySQL.
The frontend URL is working fine but when I go to the admin URL (http://localhost:8000/symdemo/admin), then my CSS URL is also redirected to the admin page meaning I am not getting CSS code into the response.
The reason as that the default PHP server does not have a URL-rewriting module and index.php is also adding to the CSS path.
How can I fix this?
I'm not 100% sure, but I think you'll find that using a webserver that supports rewrites is quicker than trying to replicate the necessary rewrites in PHP. Rewrites are listed as a requirement in Symphony CMS's readme, and last time I checked Symphony was still dependent on webserver rewrites for some of its routing/files.
You can, of course, use Apache. If you'd like to use a lighter and cleaner webserver that's easy to configure, I recommend Hiawatha, which has a Symphony URL toolkit/rewrite rule set available.

How to define friendly URL rules using .htaccess?

Long time I am trying to define Rewriting rules but I could not succeed yet? I have a simple page http://www.myURL.com/pdf.php?id=2. I want to make it friendly by this: http://www.myURL.com/pdf/2/ ...
I wrote this rule after spending time on google:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^pdf/([0-9]+)\$ pdf.php?id=$1.
When I uploaded my .htaccess file on server and try to run first it give me 500 ERROR. Second time it loaded page but it could not show me friendly URL; it was showing as before.
Kindly tell me better solution, I followed many instructions to make it useful but could not successful. My Client are not going further unless I showed him friendly URL. kindly help me as soon as possible. This is my client hosting Organization: Justhost.com.
You can do this using Apache's Rewrite Engine. Something like this:
RewriteEngine On
RewriteRule ^pdf/([0-9/]+)$ /pdf.php?id=$1 [L]
This will allow you to go to http://yoursite.com/pdf/192 instead of http://yoursite.com/pdf.php?id=192
Here is a very simple tutorial on using Apache's mod_rewrite to create friendly URLs.
Probably a bit too much to take just right now but you could look at some of the available MVC frameworks to help you solve this. I'm guessing that the host only offers PHP 5.2 so I'd take a look at the routing/controller solution from Zend Framework 1.12 to solve this as that can be implemented into existing code without too much trouble.

Need php template help and clean url help

I'm new to PHP and web development in general, but have been a programmer for 5 years. I'm trying to work on my own website from scratch using Notepad to edit my PHP files and WAMP for my server. I'm able to view my files fine in Safari, Chrome and Firefox, but not IE (which we all know IE isn't the greatest) because I'm using some HTML5 stuff. Anyways, I have an Includes folder that holds my files for my header, menu and footer. I have an index.php file that includes these files and displays them fine. In the center of the page is where I want the content. To try and keep clean urls, I made quite a few folders and put this same index.php file in there (e.g. Profile/index.php, Forums/index.php, etc.). I did this so when I went to localhost/mysite/profile/ it showed me the template I wanted to use. However, there has got to be a better way to use the template and a better way to have clean urls. I'm not currently hosting this site anywhere so I don't know if I'll have access to the htaccess file (not even sure what it is honestly, just seen it mentioned), but I was curious of having the folder structure (one folder for each menu item) is a normal or ok practice? I was also curious if there is a way to use the index.php without having to copy and paste it every time I make a small change. Any help would be appreciated. Thanks.
If you are planning on templating I suggest using an existing platform like Symfony, Zend Framework, or Smarty. Smarty is probably the easiest to get going with and it is (almost) purely for templating. Whereas Symfony and Zend Framework are big packages with lots of bells and whistles.
If I was going to be doing pure templating I would look at Smarty. I use Zend Framework for just about all my current PHP projects now but it has a pretty steep learning curve. Your first couple weeks will be frustrating.
As far as URLs go, .htaccess is probably the preferred method (at least in my book). Zend Framework and Symfony both have kind of default URL writing style that looks like http://host/controller/action where controller would be Profile or Forums. You wouldn't necessarily have to see host/profile/index, it could be host/profile or host/profile/edit, where edit is the action being performed.
Copying the index.php file is not really the way I would go. It seems messy and there are a few other options. If you want the urls to be clean and search engine friendly you should really have a look at url rewriting using .htaccess
You're saying that you're not sure if you will have a server with "access to the htaccess file" but if you can upload files you can always upload a .htaccess file as well -- the problem is that the web server is not always using them or might not have mod_rewrite enabled. In this case you can get your urls on the format of http://www.example.com/index.php?u=/Profile/foo (this is also my preferred way to handle url rewrites).
In your index.php just make sure to read the requested url parameter passed by mod_rewrite and include whatever files in the folder that you need. Actually, you don't even need a "physical" folder structure, you might want to implement it using classes or something like that. (I'm sure I would).
I would really recommend that you go have a look at a good PHP framework, CodeIgniter for example. Even if you decide to code everything from scratch, you would still learn a lot about best practices for url handling, databases, the MVC pattern, template engines, database abstraction layers and PHP coding in general.
your answer is the htaccess file, is converts the 'folder structure' to $_GET value's
for example,
if you're in website.com/Profile/ you can write an htaccess line that will convert that into website.com/index.php?folder=Profile
htaccess:
RewriteEngine on
RewriteRule ^/(.*)/$ index.php?folder=$1 [NC,L,QSA]
RewriteRule ^/(.*).html$ index.php?folder=$1 [NC,L,QSA]

Need to make one RewriteRule play nicely with another, embarrassingly weak understanding of the principles of mod_rewrite standing in the way

I have the following system:
Windows Server '03
IIS 6 (shoot me)
PHP 5.2.14
Wordpress 3.0.1
WPML 1.8.1.2
I've added Wordpress to an existing high-traffic site, in a directory we'll call
http://thehost/wordpress/
For mod_rewrite-like features, I'm using Helicon's ISAPI_Rewrite 3.1.0.76. I'm using it to enable "pretty" permalinks in the Wordpress install. The single rule is this:
RewriteRule ^/wordpress/(?!index.php)(?!wp)([^.]*)$ /wordpress/index.php/$1 [I,L]
I need to add another rule, however, and neither of my dual strategies of thinking it through and bashing it through has borne fruit.
My site is a fully bilingual one. There are no pages that aren't under one of the two following subdirectories:
http://thehost/en/ for English, and
http://thehost/sp/ for Spanish.
This is a convention I aim to maintain, even for the Wordpress install. I currently have a somewhat hacked version of WPML (Wordpress Multilingual plugin) operating, and it works such that all pages and posts—regardless of the language—include either the /en/ or the /sp/ in the URL. This has led me to believe I am achingly close—perhaps only one RewriteRule away!—to making my Wordpress URLs conform to the same URL convention as the rest of my site. My problem though, as described, is that I can't quite get it to work.
So, if I have this URL somewhere in my Wordpress install:
http://thehost/wordpress/en/2010/08/omg-this-post-is-in-english/
how can I transform it to
http://thehost/en/wordpress/2010/08/omg-this-post-is-in-english/
while not losing Wordpress's permalinks? I've managed to swap the /en/ to its correct location (^/en/wordpress/(.*) /wordpress/en/$1), but I can't get the two rules to play nicely together. That's my problem. Well, that and a few others not appropriate for stackoverflow.com.
Thanks for your time.
Try chaining the rules with the "C" Flag. Example:
RewriteRule ^/en/wordpress/(.*) /wordpress/en/$1 [C]
RewriteRule ^/wordpress/(?!index.php)(?!wp)([^.]*)$ /wordpress/index.php/$1 [I,L]

Which one is faster rewrite or folder based URLs?

I am creating URLs in my website as
http://example.com/register/
http://example.com/login/
I am creating above URLS by writing following rules in htaccess
RewriteRule register/ /register.php
RewriteRule login/ /login.php
Now its working fine for me, but if I create my URLs by creating seprate folder for /login/index.php and /register/index.php By creating folders and index.php files inside those I can achieve above functionality easily.
But I want to know which w=one will be faster, I tried both methods but not seen much difference, according to logic and apache specifications and everything which method will be faster and a good method to go with.
My friend says .htaccess rules will be slower, because in case of htaccess first it will check for rules and then it will redirect to the corresponding attached page, this process will take time than folder organization.
I know that .htaccess will at least be easier for development/debugging purposes because:
1) Because all of your files will be in the same directory instead of their individual ones.
2) Because your editor will show the actual filenames instead of multiple index.php.
Also, since you're using .htaccess, this makes it easier to put a rule in such as:
RewriteRule ^getdata/(\d+)/$ getdata.php?page=$1
Which you can't do when using the directory-based methods.
Overall, the speed difference is negligible compared to the benefits available from using mod_rewrite.
.htaccess will be slower, but the time difference will be negligible.
You should concentrate on other things - like which one is easier to maintain.

Categories