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
Related
I want to convert id to title in url address bar , i found many question related this topic .i also created .htaccess file in root directory code look like
<IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ProductDetail.php?Product=$1 [QSA,L]
</IfModule>`
But nothing work .i am new in rewrite URL, i just want to convert
ProductDetail.php?Product=1212 To ProductDetail/Title
Thanks for your kind regards
One basic rule for url rewriting is that it is not possible to process the query while rewriting it
If in your example ProductDetail/Title Title means the title of the item having the product=1212 we need to actually run the query to find the Title before even generating the url, which in turn will lead to change in the query method of your website and thus making it less efficient (Instead of using the product in the where clause you have to use a string which will replace the Title).
We can however create the transformation from ProductDetail.php?Product=1212 to ProductDetail/1212 with the following code
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ ./ProductDetail.php?Product=$1 [QSA,L]
</IfModule>
Also, you need to precede the php file with a ./ in your path as php file resides within a directory. Otherwise it might generate an error or it won't work at all.
Regards
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 am looking for your recommandations on what would be the best way to implement friendly URLs.
What I currently do is redirect all 404 requests to folders or files that do not exist to index.php.
index.php reads the query string and makes a database call to see if the url is in the page_urls table then based on the page type fetches content etc etc.
The .htaccess contains the following lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
Is there a more "clever" way of doing this please? Thank you.
Thank you.
The best way I've found is to do something like the following:
RewriteEngine on
RewriteRule ([a-zA-Z0-9_-]*)\.html index.php?page=$1 [L]
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
Every single time a user registers on my site I would like them to have their own subdirectory with their registered "username". Every user subdirectory will have the same "index.php" file which will do something.
For example: "/users/username1/" and "/users/username2/"
If some one wants to access the subdirectory they would simple go to:
"www.example.com/users/username1/" or "www.example.com/users/username2/"
The easy and messy solution would be to simply create a subdirectory for every user and place the same "index.php" file in every directory. But to me this is only going to crowd my server space and make my directories large.
I wanted to know if all this can be done using .htaccess? Can I create one "index.php" and one ".htaccess" file and place them both in my "/users/" directory? What would be the actual code that I would have to place in my .htaccess file??
If you have a better way of doing this please let me know. I am using Apache and PHP as my working environment.
Thank you
Well, for example, you could do it all with one htaccess like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
What it does:
switches on rewrite engine
checks if a requested file exists
checks if a requested directory exists
if NOT, it redirects request to your main index.php
Basically that means if you enter url such as yourdomain.com/users/ivan/, you request will be redirected to:
index.php?url=/users/ivan
then you $_GET['url'] in your index.php and split it into pieces.
That's just an example, there other mod_rewrite methods to do this.
Make it virtual. There are no subdirectories, you can use mod_rewrite to simulate that.
With mod_rewrite you can make /users/username1 lead to /users.php?user=username1 for instance. Everything is transparent for the client, he wont notice what is really happening.
By using something like this:
RewriteEngine On
RewriteRule ^([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
You can customize RewriteRule as much as you want.
You can essentially type in any directory you want, and it will be redirected to your index.php page.
If you want to make sure the existing directories are not redirected, do this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
If you want to limit the scope, so only a subdirectory of user/ is redirected (similar to Stack Overflow), simply add in 'user' to the start of the rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
And finally, if you want to have an individual file handle all user requests seperate from your actual index.php page:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([\-_0-9A-Za-z]+)$ users.php?a=$1 [L]
This is a very similar setup I use to distribute CSS files.
Note: The Directory will be contained is $_GET['a']