Using URL Rewrite and HTTP together - .htaccess

I'm trying to use URL rewriting, but only on a part of the address, though I'm new to this area of web development and I'm a bit confused.
The address is: domain.com/create.php?member=-1&hangout=1
But I'm trying to rewrite it to this: domain.com/create/?hangout=1
I'm getting the correct page, though I can't access "hangout" with $_GET['hangout'].
This is my rewriting code in my .htaccess document:
RewriteEngine On
RewriteRule ^create/ create.php?member=-1
Thank you.

If you add your own query string in a RewriteRule, you must use the QSA flag to append an existing query string
RewriteRule ^create/ create.php?member=-1 [QSA]
This will rewrite create/?hangout=7 to create.php and add both member=-1 and hangout=7 as the query string.

Related

htaccess rewrite to capture filepath and query string separately

I'm trying to get 2 different parts of a url and use them in a rewrite but I can't get it to work
I'd like
http://example.com/account/test-page?h=1&t=2
http://example.com/account/test-page
to rewrite to
http://example.com/page.php?path=account/test-page&h=1&t=2
http://example.com/page.php?path=account/test-page
I've tried a dozen different ways - this is the latest one :
RewriteRule ^http:\/\/example.com\/([^\?.]*)[\?]?([^/]*)$ http://example.com/page\.php?url=$1&$2
but it doesn't work and I'm tearing my hair out !!
could someone tell me where I'm messing up please ?
Please use the following rule instead:
RewriteRule ^(.*)$ /page.php?path=$1 [QSA,L]
The key, here, is the QSA flag, which appends in query string used to the query string already passed to page.php.
To be clear, a request made to /account/test-page?h=1&t=2 will be internally rewritten as /page.php?path=account/test-page&h=1&t=2.

htaccess rewrite except one parameter

I want to make some url rewrite with htaccess,
http://www.example.com/article.php?post_id=123&title=2014-03-02-today-s-trends
=>
http://www.example.com/article/123/2014-03-02-today-s-trends
but some special rule. if some article click from rss or social share. I want to:
http://www.example.com/article.php?post_id=123&title=2014-03-02-today-s-trends&source=rss
=>
http://www.example.com/article/123/2014-03-02-today-s-trends?source=rss
my rewrite code here
RewriteRule ^article/(\d+)/(.*)\?source=(.*)?$ article.php?post_id=$1&title=$2&source=$3
but get 500 error. How to do it in a right way?
You can just use the QSA flag so that it will append the query string with it if source=rss is on the URL.
Not sure how your ogininal rewrite is but you can try this. It should work with or without source=rss
RewriteRule ^article/(.+)/(.+)$ article.php?post_id=$1&title=$2 [QSA,L]
Then in your code you can check for the source key in the $_GET.

.htaccess dynamic to static URL

I'm trying to make my dynamic URL's into static looking URL's.
This is a typical URL that I now have:
http://www.somedomain.com/design/index.php?p=about
I would like it to be: http://www.somedomain.com/about
So far, I've gotten it to look like this: http://www.somedomain.com/design/about.html
This is the Rewriterule I'm using: RewriteRule ^([a-z]+).html$ index.php?p=$1 [L]
How would I modify it so it would look like this: http://www.somedomain.com/about?
Thanks for any/all help!!!
Very much appreciated!
Using rewrite rules to give 'static' URI is NEVER a good idea.
A few other ideas you can use:
Make the 'about' page a directory (folder) with a file called index.php or index.html in it. This way the URL shows http://example.com/about/ and the information you wish can still be displayed as needed.
Use the POST method instead of GET methods. This will display as http://example.com/about.php (Note: there is no ? or other parameters behind that.)
Utilize both methods to give a 'seamless' URI on page transitions.
Rick, you're on the right track. You need to read the Apache rewrite documentation. For your docroot/.htaccess start it with:
RewriteEngine On
RewriteBase /
Then generalised version of your rule:
Rewrite Rule ^(\w+)$ index.php?p=$1 [L]
This will rewrite any requests which are for a word string to index.php. You need to be aware that the rewrite engine rescans the .htaccess file if a match has occured so you need to make sure that you don't create a loop. In this case the replacement string has a "." in it and the pattern doesn't, so this won't occur, but for more complex cases you may need to 'guard' the rules with one or more RewriteCond statements. Again, read the Apache documentation.

.htaccess Rewrite Based on Existence of Path in URL

Here's the scenario, I have a website that used to be a static HTML site and WordPress blog using a subdomain (http://blog.domain.com).
I recently combined everything into a single WordPress installation. To maintain old links I had to rewrite requests like "http://blog.domain.com/index.php/2010/10/16/post-name" to "http://domain.com/index.php/2010/10/16/post-name". My problem is that when trying to visit just "http://blog.domain.com", I get redirected to "http://domain.com" when I want it to go to "http://domain.com/index.php/blog".
So, if a user requests "http://blog.domain.com" (by itself, with or without slash), I want it to go to "http://domain.com/index.php/blog". If they request an old URL of "http://blog.domain.com/some-link-to-a-post", I want it to redirect to "http://domain.com/some-link-to-a-post". In other words, if it's a URL to an actual post, I just want to strip the "blog" subdomain. If it's the old link to the main blog page, I want to remove the "blog" subdomain and append "/index.php/blog"
http://blog.domain.com/ -> http://domain.com/index.php/blog
http://blog.domain.com/index.php/2010/10/16/post-title -> http://domain.com/index.php/2010/10/16/post-title
Hopefully that's clear. I'm not an htaccess expert, so hopefully someone can help me out here. Thanks in advance!
Using the [L] command at the end of a rewrite will tell htaccess that this is the last rule it should match. If you put a rule to match your first condition at the top and the other rewrite rule you said you had already created after it, you should get your expected result.
Try this:
RewriteRule ^blog.domain.com(/?)$ domain.com/index.php/blog [L]
# Your other rewrite here #
I couldn't get that solution to work. However, I used the following:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/index.php/blog/$1 [R=301,L]
That ends up in a URL like http://domain.com/index.php/blog/index.php/2010/06/04/post-title, but Wordpress is smart enough to fix it.

.htaccess and url

Is it possible somehow using mod_rewrite change the url from http://www.mywebsite.com/company/123/reviews to http://www.mywebsite.com/company-123/reivews?
It's not a redirect. The problem is that the real path is the first one and I need my browser to display the second path. So when the user goes to company-123/reviews the content of the page is displayed from company/123/reviews.
Thank you.
RewriteRule ^/([a-z]*?)-([0-9]*?)/([a-z]*?)$ /$1/$2/$3
I think this will work, the regex does it's job atleast.
Use this rule to rewrite the former URL path to the latter one:
RewriteEngine on
RewriteRule ^([^/]+)-([0-9]+)/([^/]+)$ $1/$2/$3 [L]
But you already need the former URLs in your documents to get this rewriting work.

Resources