can't figure out mod_rewrite for certain pretty url - apache

I am having trouble figuring out how to write this condition I am wanting in my .htaccess file.
I have tried multiple different ways with no success but, here is what I am trying to accomplish. Using apache.
My current url is like this: http://site.com/page?id=123&variable=a-text-like-this
I am wanting to turn it into this: http://site.com/differentpagename/a-text-like-this/
I want to hide the name of the real page and the id and its value.
can someone please help ive spent all night trying to figure this out.

This will do what you want. But beware, that id is hard-coded in .htacess, which is very bad. So, you'll need a line for every alias. That's why I asked about transfering alias to id in the script.
RewriteEngine on
RewriteRule differentpagename/a-text-like-this/ /page?id=123&variable=a-text-like-this [L]

Related

Rewriting Link with mod-rewrite

I have a link being sent to users in one format, but I need to make sure it passes through a main index page for login purposes. I figured mod rewrite was the way to go.
Link being clicked on by user:
https://sub.domain.com/link/link.jsp?pageId=1234567&id=12345
Where it needs to go:
https://sub.domain.com/index.html?o=(full original URL from above, including query string)
The o= in this case will let the user login and pass them along to that original URL. Now, there are also some images and a style sheet involved, so I need to have the rewrite ignore them.
After reading documentation and a number of code examples (many from this site), I tried to just get a basic code going to see if my rewrites will even work, as I'm new to this.
This appears to force the URL to rewrite but isn't passing the user along to the original link:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub.domain.com [NC]
RewriteRule .* https://sub.domain.com/index.html?o=%{HTTP_HOST}%{REQUEST_URI}?%{QUERY_STRING} [L]
Also please note this is all going inside a virtual host, not htaccess. As I said, the code above appears to redirect when I test it, but it might be unintended and not at all how I should write it.
I also tried adding in this code for images/stylesheet ignores
RewriteCond %{REQUEST_URI} !\.(css|jpg|jpeg)$ [NC]
At that point everything went crazy, but I know it's because I'm slapping code together and thinking it's going to work. From that point I tried a lot of changes, but most resulted in a loop condition and I kept falling back to square one (original code you see above). Apologies for the long winded post, but I'm hitting my head against a wall at something I thought wouldn't be difficult. Obviously, despite reading, I'm lacking some understanding. Any guidance would be very helpful.
The problem with your current solution is: an URL is only allowed to have one ? inside.
Note that your code will send a redirection status code to redirect inside the browser, because you provided a FQDN. Try doing an internal redirect by just using /index.html?o=.... If that doesn't work either (not sure right now), URL-Encode the second ?
[Edit]: rewrite, guess I got the question wrong.

Can someone prompt me in the right direction to use mod_rewrite?

I am a bit new to this but I am trying to learn. I know that in PHP we can have a URL that ends in ?id=something but it seems to me that some websites are doing it just forward slashes. Is this possible using PHP? I saw these questions that seem to say it is possible but I haven't cracked it yet.
Can someone prompt me in the right direction for mod_rewrite? I would like to change the following URL
http://www.example.com/all-products/?onsale=true&topic=health&item=adaaos45
into
http://www.example.com/all-products/items-on-sale/health/adaaos45
Can you point me into the right way?
What you are looking for is Apache Url Rewriting.
I will break it down a bit for you to help you understand, it helps to know a bit of regex.
there are a lot of answers here that discuss the method, but to sum it all up, you need to do three things.
# Switch on URL Rewriting
# Choose the URL that you want to display
# Point it to the true URL that gives the information.
Options FollowSymLinks
RewriteEngine On
RewriteRule ^all-products/items-on-sale/health/adaaos45/?$ all-products/?onsale=true&topic=health&item=adaaos45 [NC,L]
Now of course, if you would want to match any results for the variables, you need to match the word in regex, and remember it, and use it in the last part of the line. But this should get you started on understanding what is going on.
With this code in your .htaccess, browsing to
http://www.example.com/all-products/items-on-sale/health/adaaos45
will show you the content that displays on this page.
http://www.example.com/all-products/?onsale=true&topic=health&item=adaaos45

Simple mod_rewrite, replace one word in every instance

I've been looking for an answer to this forever and can't find it, yet it seems like it should be so simple!
I want to use mod_rewrite to replace a word in a url in every instance that it shows up, but I don't want a redirect to happen, just changing the way the url appears to site users.
Example:
Change
mysite.com/something/groups/anything...
to:
mysite.com/something/projects/anything...
I know I could go through and start tweaking files but mod_rewrite would work much better because I'm sure I'll mess something up otherwise (for reference I'm using joomla/jomsocial).
RewriteEngine On
RewriteRule something/groups/.*$ something/projects/$1

apache mod_rewrite: using database to update rewrite rules

Total newbie at mod_rewrite.
Let's say I want to create nice URLs for every manufacturer on my site,
so I have
www.mysite.com/samsung
www.mysite.com/sony
www.mysite.com/acme
works well enough.
However, if I have hundreds of manufacturers and if they're changing constantly, what then? There are some vague references for something called rewrite map somewhere but nothing that explains it and no tutorials. Can anyone help?
Also, why is this problem not the main topic covered in tutorials for mod_rewrite? How is mod_rewrite possibly useful when you have to maintain it manually (assuming you have new content on your site once in a while)?
There is also mention of needing to have access to httpd.conf
How do I access httpd.conf on my hosting provider's server? How does every other site do this?
Thanks
Just came across this answer while searching for a similar solution — searching a bit further I discovered that mod_rewrite now has the RewriteMap directive, which will do exactly what you want without the need to run PHP or another scripting language.
It lets you define a mapping rule with a text file, a DBM file, an external script or an SQL query.
I hope that helps!
The way this would typically be done is that you would take all URLs that match a specific pattern and route them to a PHP file (or whatever your server-side programming language is) for more complex routing. Something like this:
RewriteRule ^(.*)$ myroute.php?url=$1 [QSA,L]
Then, in your myroute.php file, you can include logic to look at the "url" query string parameter, since it will contain the original URL that came in. Perhaps you could match it to a manufacturer in the database, or whatever else is required.
This example obviously takes all URLs and maps them to myroute.php. Another example might be something like:
RewriteRule ^/manufacturers/(.*)$ manuf.php?name=$1 [QSA,L]
In this case, it will map URLs like so:
/manufacturers/sony => /manuf.php?name=sony
/manufacturers/samsung => /manuf.php?name=samsung
etc...
In this case, your manuf.php file could look up the database based on the name query string parameter.

.htaccess - route to selected desination but change browser url

Problem:
I'd like to accept the original request. Say its, /IWantToGoHere/index.php
but I want to return to the browser, /GoHere/index.php
To be clear:
I actually want to send the original request location down to the script requested, however, I want to return the user a browser URL to another destination.
Code:
RewriteEngine on
RewriteRule ^(.*)IWantToGoHere\/\.php$ GoHere/index.php [NC,C]
RewriteRule ^GoHere/index.php$ GoHere/index.php [R,NC]
Notes:
I realize the code above doesn't work. I've tried a number of different calls. I spent umpteen hours yesterday trying every clever solution I could pull out of my limited mod_rewrite knowledge bank. Based on my understanding of mod_rewrite, I don't think it's do able. I understand its not what the preprocessed was designed to do. At least not from anything I could find on the Apache web site. I've been told that if I could dream it up, that it could be done:) I was wondering if anyone had and ideas how to get it to work.
Why would you want to do that?:
Because I do. No really, I want the URL returned to the user for further processing.
weez
If I understand the question correctly, to accomplish this you'll need to send a header from /IWantToGoHere/index.php that redirects to /GoHere/index.php once the script is finished executing. That is, if you want Apache to still call IWantToGoHere but return to GoHere. So at the end of processing for IWantToGoHere script something like this:
header('Location: /GoHere/Index.php');
Which will redirect correctly.