Simple modrewrite, how does a link appear? - apache

When you use modrewrite to rewrite your urls, when does the rewrite occur. Will the user be able to see the url before rewrite, when hovering over the link? When they hover over a link will their browser display the rewritten url or the url before it was modified with modrewrite?

The rewrite is done in the server so the user will never know,
PHP also doesn't know what link its pointing to only the script file

mod_rewrite acts on the server side, meaning that apache rewrites incoming URLs before responding to the request. Any HTML links you add to a page will point to the URL you entered, as mod_rewrite doesn't modify any outgoing data.

The rewrite occurs when Apache has parsed the request - before the PHP interpreter has been started.

Related

remove http query parameter from url htaccess

I want to shorten my URI to just my site address that is
http://www.abcdef.org/index.php?pg=23
http://www.abcdef.org/histop.php?pg=1
http://www.abcdef.org/other.php?pg=29
to
www.abcdef.org
How can I rewrite this using my `.htaccess file? I read a lot of articles but could not find any solution.
Use Redirect directive
Redirect /foo.html /bar.html
Apache Documentation
But understand when the user types in whatever URL it will map to your index file in your case. But you can't just hide every URL to the domain name...it doesn't work that way.

Redirecting from example.com to www.example.com

My site uses AJAX, and it seems like I have to include the full path when I use it to access a function. This is fine, as I can code it in. The problem is, I have hardcoded http://www.example.com... but if a user has gone to http://example.com (without the www) this gives an access problem and the AJAX won't execute.
I think the easiest way to resolve this would be to make sure that if a user goes to mysite.com, they are redirected to www.example.com.
I can find solutions online, but they all involve the htaccess file, which my server doesn't support - I have to use rewrite.script instead.
How can I do this using rewrite.script, or is there an alternative way to approach this?
Thanks for the suggestions - I was directed to this: http://seo-website-designer.com/Zeus-Server-301-Redirect-Generator which created the rewrite.script file I needed.
In .htaccess found in your root document:
Redirect http://example.com/ http://www.example.com/
If there is no support for .htaccess on your server, you may have to include meta tag redirect on the head of your page. or using javascript to check the URL source then redirect it.

Understanding difference between redirect and rewrite .htaccess

I'd like to understand the difference between redirecting and rewriting a URL using .htaccess.
So here's an example: Say I have a link like www.abc.com/ index.php?page=product_types&cat=88 (call this the "original" url)
But when the user types in abc.com/shoes (let's call this the "desired" url), they need to see the contents of the above link. To accomplish this, I would do this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)shoes(.*)$ index.php?page=product_types&cat=88
Nothing wrong with this code and it does the trick. However, if I type in the original url in the address bar, the content comes up, but the url does not change. So it remains as www.abc.com/index.php?page=product_types&cat=88
But what if I wanted the desired url (/shoes) to show up in the address bar if I typed in www.abc.com/ index.php?page=product_types&cat=88? How would this be accomplished using .htaccess? Am I running into a potential loop?
Some of the explanation can be found here: https://stackoverflow.com/a/11711948/851273
The gist is that a rewrite happens solely on the server, the client (browser) is blind to it. The browser sends a request and gets content, it is none the wiser to what happened on the server in order to serve the request.
A redirect is a server response to a request, that tells the client (browser) to submit a new request. The browser asks for a url, this url is what's in the location bar, the server gets that request and responds with a redirect, the browser gets the response and loads the URL in the server's response. The URL in the location bar is now the new URL and the browser sends a request for the new URL.
Simply rewriting internally on the server does absolutely nothing to URLs in the wild. If google or reddit or whatever site has a link to www.abc.com/index.php?page=product_types&cat=88, your internal server rewrite rule does absolutely nothing to that, nor to anyone who clicks on that link, or any client that happens to request that URL for any reason whatsoever. All the rewrite rule does is internally change something that contains shoes to /index.php?page=product_types&cat=88 within the server.
If you want make it so a request is made for the index.php page with all of the query strings, you can tell the client (browser) to redirect to the nicer looking URL. You need to be careful because rewrite rules loop and your redirect will be internally rewritten which will cause a redirect which will be internally rewritten, etc.. causing a loop and will throw a 500 Server Error. So you can match specifically to the request itself:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=product_types&cat=88
RewriteRule ^/?index.php$ /shoes [L,R=301]
This should only be used to make it so links in the wild get pointed to the right place. You must ensure that your content is generating the correct links. That means everything on your site is using the /shoes link instead of the /index.php?page=product_types&cat=88 link.

Force entry url

For SEO purposes. How do I force user gets redirected to mysite.com/index when types mysite.com on browser ?
I've tried Redirect / en/index
But I'm getting "Firefox has detected that the server is redirecting the request to this address in a way that will never end."
Also, can this redirection consired set language (php psession, cookies?)
p.s. xampp/windows
Redirect works on path prefixes. Redirect / … matches any path its prefix is / (so virtually any path). Use RedirectMatch instead:
RedirectMatch ^/$ /en/index
I believe the error you're getting from Firefox is because you're entering an infinite loop with this redirection. If you try to redirect to a page that doesn't exist, and your server is set up to redirect to a page that doesn't exist on 404, you'll enter a loop. So, ensure that your redirect is going somewhere significant. Does /index exist? Try redirecting to Google instead and see if you get the same error.
(My server knowledge is limited to goofing around while working on school projects, so take anything I say with a grain of salt)
This probably means you have an error in your redirect code. What Firefox is detecting is that the page are trying to view is redirecting you to itself. Without this protection it would just seem like the page wasn't loading.
If you are running windows on your workstation, I would recommend downloading Fiddler2. It will let you see the series of redirects that your server is sending.

rewrite url without change address

I want to show a page instead of another page, without change the second page url.
I know this is possible with htaccess.
I copied that code from a cms htaccess:
RewriteRule ^event-([0-9]+)\.html$ calendar.php?action=event&eid=$1 [L,QSA]
with that code, we will redirect to calendar.php?......... but I want it redirect (without changing the address in address bar) to another site, for example to http://www.google.com/page......
Is it possible?
Thanks ..
If you want to provide content from another site without changing the address in the address bar of the browser that mean you becomes a proxy.
So check apache documention for proxy configuration (this can be done for specific urls only). Even mod_rewrite can do the proxy things with the [P] tag, mod_rewrite will allow a lot more 'specific url' filtering.
Now the job of a proxy, when he have the response from the distant website and he needs to render it for the HTTP client, is only to change the HTTP headers in the response. So only url in Location tags or such specific headers will be altered. You must known that all the HTML content from the distant website will not be altered (the inner links will be on www.google.com and not on your www.whythehelldoiproxygooglewithmysite.com).
If you want to alter this returned content check mod_proxy_html module, this will add some extra stuff before sending the resonse, to do some more reverse proxy alterations.