I'm new to mod-rewrite and I have tried to mod-rewrite this url with no success.
URL structure like this :
http://mysite.com/script.php?id=15751890&xp=862297&wm=1721&ls=2725&he=63530&ks=23050&eath=53588&tk=10&ck=john&rk=37
I want to mod-rewrite it to :
http://mysite.com/script.php?id=15751890
Or :
http://mysite.com/15751890/862297/1721/2725/63530/23050/53588/10/john/37
I followed this http://wettone.com/code/clean-urls and this http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/
But just can't do it write
What to type in htaccess file
Well, if you really want to write such a rewrite you should be able to use this in your htaccess file. There are probably better ways to handle the URL's however, I'm giving you what you asked for.
NOTE: I'm going off of the URL you provided.
RewriteEngine on
RewriteRule (.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*) script.php?id=$1&xp=$2&wm=$3&ls=$4&he=$5&ks=$6&eath=$7&tk=$8&ck=$9&rk=$10&%{QUERY_STRING}$ [L]
So you can use this type of URL with that rewrite.
http://mysite.com/15751890/862297/1721/2725/63530/23050/53588/10/john/37
Refer to my answer to this question:
php, handle unique URLs
This will give you this:
http://mysite.com/15751890/862297/1721/2725/63530/23050/53588/10/john/37
Instead of trying to figure out every rewrite combination possible, you should learn the MVC routing method.
This way, you route everything to a PHP router, and then direct the request to controllers, etc, as needed.
HTACCESS to rewrite everything:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
In your PHP script, you can use: $_SERVER['REQUEST_URI'] to get the requested URI.
Example: http://example.com/foo/bar/95 would rewrite to index.php, and $_SERVER['REQUEST_URI'] would then be /foo/bar/95 - and then you route accordingly.
Typically, your URL structure would be like:
http://example.com/controller/action/param_1/param_2/param_3/etc
Whereas "action" is just another name for a controllers method. How you code it is up to you.
Related
I'm currently writing a PHP app and am about to create various controllers. Currently I have one controller with certain method, so the URL looks like this:
http://somewebsite.com/index.php?c=controller&a=action
It's being rewritten to this:
http://somewebsite.com/controller/action
With this .htaccess file:
RewriteEngine On
RewriteRule ^([a-z]+)/([a-z]+)$ index.php?c=$1&a=$2 [NC]
What I want to achieve is the ability to rewrite URL with more than one controller (the more, the better), possibly in random order. Is there a more convenient way than rewriting every possible combination of URL parameters?
Many frameworks (Codeigniter, WordPress, Laravel, etc.) use an .htaccess file similar to the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
This rewrites all incoming URLs to be handled by the index.php file. You can then use $_SERVER['REQUEST_URI'] variable to get the exact request URI, parse it, and then handle it how you want.
if you're going for an arbitrary routing scheme, probably your best bet is to just pass the entire url string e.g. ^(.+)$ to index.php and let your php application split the string based on '/' and handle the array however makes sense for your application
I'm sorry if there is already a question on this I just couldn't find it since I don't know what it is called.
Basically, I want to be able to access certain posts using their id without using GET. For example, I would like to access a post using website.com/post/crazy-clickbait-post instead of something like website.com/post.php?id=crazy-clickbait-post. It's just more user-friendly from an advertising perspective.
How would I go about this? I was thinking that maybe there would be a way to use a main post page in the post folder called something like index.php and then that would parse the url to get the post id but I'm not sure if that's possible.
You basically want 'SEO friendly' URLs like the following topic:
How to write htaccess rewrite rule for seo friendly url
In your case you need to add this (or create) to your .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^post/([^/]+)$ /post.php?id=$1 [L]
Im trying to pass some variables from the URL to the PHP script. Now I know that www.site.com/index.php?link=HELLO would require $_GET['link'] to get the variable "link". I was hoping there are other ways to do this without the variable.
For instance I want a link structure like this: www.site.com/HELLO. In this example I know that I have to create a Directory called Hello place an index file and it should work but I don't want to create a directory and Im hoping there's a way to "catch" that extra part after the domain. I'm thinking of creating a custom HTTP 404 Page that will somehow get the variable of the not found page, but I don't know how to get the HTTP 404 error parameters. Is there another simpler way to get a variable without the use of the extra ?link= part? I just want it to be a structure like this www.site.com/HELLO.
What you want is URL rewriting. You don't mention what kind of web server you're using, so I'll assume it's Apache.
If you have mod_rewrite enabled on your web server, this can easily be accomplished by creating a .htaccess file in your document root with the following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1
This will make all requests - that match the regular expression [a-zA-Z0-9]+ - get forwarded to index.php. For instance, if you try to access domain.com/hello, PHP would interpret this as trying to access index.php?request=hello.
You can read more about this in the Apache HTTP Server manual about mod_rewrite.
In which case, you normally use .htaccess to alter the URL in some form. For instance:
RewriteEngine On #Enable Rewrite
RewriteCond %{REQUEST_FILENAME} !-f #If requested is not an existing file
RewriteCond %{REQUEST_FILENAME} !-d #If requested is not an existing directory
RewriteRule ^(.*)$ /index.php?link=$1 [L] #Preform Rewrite
Which will make www.site.com/hello to www.site.com/index.php?link=hello. This change is invisible to the user (he will still see www.site.com/hello as an address). Be advised that it may cause trouble if you try using relative paths with CSS/JavaScript files.
You need to use URL rewriting to do this. If you're using Apache: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
you can do that with a mod-rewrite structure.
Here's a pretty good tutorial on how to set it up with php/apache.
http://www.sitepoint.com/guide-url-rewriting/
We all know that wordpress have simple .htaccess code like below
RewriteEngine on
RewriteBase /
# only rewrite if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-s
# pass the rest of the request into index.php to handle
RewriteRule ^(.*)$ /index.php/$1 [L]
But if I redirect all requests to index.php, I think it becomes pretty cumbersome to handle every rewrite in php. Currently I have a logic in mind, like maintain a db table with all valid redirections. But still I don't know how to handle rules likes ([0-9]+).
If anyone has implemented something like this before or has a logic in mind, can you please guide me in this matter
The sole purpose am doing this, is because I want flexibility in adding/deleting categories in the menu of my site. I don't want to go to .htaccess every time and edit it at all places. I want to create more like a CMS where user can add delete categories
My suggestion would be to set up php based routing the basic idea is that you do something like this:
RewriteEngine On
# skip all files with extensions other than .html
# this way if you want you can append the .html to dynamic pages
# which wont really exist as a file
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]
// redirect everything to your front controller
RewriteRule ^(.*)$ index.php [QSA,L]
Notice how you just make everythign go to index.php but you dont rewrite any of the variables or anything. This is because you will work out what to based on the pattern of the URL with php. In order to do this youll need to implement a router. Basically a router takes a request and matches it against a pattern and then determines any parameters based on the pattern.
There are existing libraries out there to do this for you. For example Zend_Controller_Router.
I don't understand the question, WordPress already handles all this for you. Unless you mean you aren't using WordPress? In which case yes, you can do it either way. What kind of URL structure do you want? You could write a rule like so:
RewriteRule ^category/(.*)$ categories.php?cat=$1 [L]
To make a URL like domain.com/category/dogs rewrite to domain.com/categories.php?cat=dogs. Obviously you can adjust this to your liking, and write a few more similar rules for tags, posts etc.
Handling routing in php would be a more dynamic and 'elegant' solution. You could try using a framework like CodeIgniter, this will manage routes for you automatically and make it easy to define custom routes. Probably better than writing a bunch of .htaccess rules.
Wordpress Experts:
So, my wordpress instance is currently set to use friendly urls. My current rewrite rules look something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Which basically means: send everything to index.php for dispatching.
What I NEED to do, is whenever someone accesses my page /promo, I send the rest of the uri to that controller (which is a file promo.php attached to the page via Template Name: Promo) but it would still need to be piped through the index.php to do that.
In a nutshell, I want /promo/fakecompany to technically behave like this to wordpress: index.php?page=promo&fakecompany
Or even a redirect that would take the second segment of anything beyond /promo and create a querystring out of it i.e. a dynamic kind of this:
Redirect 301 /promo/fakecompany /promo/?fakecompany
Redirect 301 /promo/fakecompany/referpage /promo/?fakecompany&referpage
Is there a way to do this? Or possibly a more elegant solution? Worst comes to worse, I'm just going to have to hardcode in a redirect for the moment:
Thanks in advance.
Your current rules say : redirect anything that is not a regular file or a directory to index.php
For your request :
RewriteRule ^/promo/([^/]+)$ index.php?page=promo&$1 [L]
or
RewriteRule ^/promo/([^/]+)/([^/]+)/$ /promo/?$1&$2
Should work (provided you place it before the current rules)