Ignore last part of URL with .htaccess - .htaccess

We have a FAQ page /faq (tab style) where every question should have its own 'ghost' url/page. So users could visit eg.
/faq/question-1
/faq/question-2
/faq/question-3
The problem is question-1, question-2, question-3 are not actual pages but just sections on /faq. For SEO, aesthetics and usability reasons we do not want to work with ?q= or #
I've searched and tried every .htaccess thread I came across but without result.
Is there a way we can show the page/faq when visiting /faq/question-1 and keep the url /faq/question-1 with mod_rewrite? (we cannot hardcode it because we do not know all future question slugs) So basically something that tells the browser: if the first url part is /faq/, just ignore everything that comes behind but keep the url.
Thanks

This is a trivial rewriting task and it is unclear why this should not work for you:
RewriteEngine on
RewriteRule ^/?faq/.+ /faq [END]
Since you claim that you "tried every .htaccess thread you came across" and this clearly works the question is: why not in your setup? But since you did not tell us anything about your setup we cannot really offer more help...
These are some general hints though which you should go through:
Where did you implement the rules you tried? In the http server's host configuration or in a distributed configuration file?
If you are using a distributed configuration file (".htaccess") then how did you make sure such files are interpreted by your http server and how did you test that?
Did you check your http server's error log file for hints?
Did you make sure that you are not actually looking at cached responses? So did you really test with a fresh anonymous browser window using a "deep reload"?
Since the CMS you are using requires own rewriting rules, where did you add those rules you tried? Remember: the order is important!

Related

Use .htaccess redirect to keep original url but show content of redirect

I want people that visit the URL /this-url.html get to see the Site of /that-url but to keep the url as /this-url.html
I already tried various solutions of stackoverflow but none of them worked for me. I tried to use the [P] flag, the [QSA] flag and no flag at all.
My basic code:
RewriteEngine on
RewriteRule ^/this-url.html$ /that-url
The redirect always works, but it also changes the URL. I need to keep the URL as /this-url.html
You forgot to consult the most important source of information when you want to learn how to use some tool or utility: the documentation. That is, as typical for OpenSource projects, of excellent quality and comes with great examples. You really should start looking into it: https://httpd.apache.org/docs/current/mod/mod_rewrite.html
Here is a slightly modifed version that should do what you want:
RewriteEngine on
RewriteRule ^/?that-url$ /this-url.html [END]
The main difference: in dynamic configuration files RewriteRules operate on relative paths, you were trying to match against absolute paths, which would only work for rules implemented in the real http server's host configuration. The version I posted works likewise, so it is more robust.
If you receive an internal server error (http status 500) using above rule then chances are that you operate a very old version of the apache http server. In that case try using the old L flag, it will probably work the same here, though that actually depends on the specific situation you are in.

HTACCESS: Redirect all the requests to another page, in place of index, hiding arguments?

I expose here my issue, I hope to explain myself clearly and correctly. In case of any specification, please ask me.
What I need is to redirect all the request (except the one to index.php) to another .php file, without the necessity to specify an argument.
here an example of what I need to do:
http://www.example.com -> shows index.php
but:
http://www.example.com/songs/title-of-the-song
should call the page: songs.php?dir=title-of-the-song of course without showin to the user the string "songs.php?dir=title-of-the-song" but just the URL
http://www.example.com/songs/title-of-the-song
It's what occurs with Drupal, but i'm not using that CMS for my site.
You are probably looking for something like that:
RewriteEngine on
RewriteRule ^/songs/(.+)$ /songs.php?dir=$1 [L]
Note: this is the version for the http host configuration. For usage in .htaccess style files you have to adapt it slightly. But if you have access to the host configuration you should always prefer that over .htaccess style files. Those files require an additional module, are notoriously error prone, hard to troubleshoot and really slow the server down.

Redirecting to another site by masking url

I want to redirect all the users who enters www.siteA.com/folder1 to www.siteB.com/folder. But user should see www.siteA.com/folder1 in address bas even after redirection to www.siteB.com.
I do not know how to do it with .htaccess Can somebody please help me how to mask the url. I really need your help.
Thanks
You can "Force the substitution URL to be internally sent as a proxy request" via the P flag.
RewriteRule ^folder1(.*) http://www.siteB.com/folder$1 [P]
There are some other examples in the documentation of the RewriteRule.
If this does not work (e.g. no access to the server configuration and the proxy module is disabled) you are probably best of using a Proxy script like PHProxy.
On a second look I think PHProxy is not really what you need. Maybe give this one a try: http://code.google.com/p/php-proxy/ - The installation instructions look pretty simple.
You want to use proxying . On Apache, mod_proxy should do what you are looking for.
Bear in mind that your server will eat up twice the amount of bandwidth. Once to get the data from the source server and again to send it to the client.

Dynamically creating URLs for other websites

I'd like to know how websites have created URLs with other domains like these on trafficestimate.com.
I'm guessing it's some .htaccess stuff to redirect domain names to a dynamic page?
Thanks
Your URL has an GET Request. So when someone calls the page http://google.com/search with the parameters hl=en, safe=off etc., the page can process those parameters. So for instance safe=off means that you want to get back any search result. The q=site:... is your search string. In this case Google will look it up in its database and give you the results. So when you call this URL there is probably no .htaccess processing done. However you can process the URL and GET request with .htacces and i.e. redirect the user to another page.
Maybe you'll describe a bit further what exactly you trying to do/want to know. This makes explaining easier.
EDIT: After reading Gumbo's comment I looked at the Google result page. So maybe your question means the trafficestimate-URLs. They look like http://trafficestimate.com/example.org. This is really a good case for .htaccess. So using .htaccess they take the URL and redirect it to http://www.trafficestimate.com/websites/?domain=example.org. Here you have again a GET request and an application builds the page.
Some URL rewriting is probably involved. Otherwise they would have to create an existing file for every possible request.
Using Apache’s mod_rewrite in a .htaccess file is one option. But since the server identifies itself with “Microsoft-IIS/7.5”, they are probably rather using ISAPI_Rewrite, a mod_rewrite derivative for Microsoft’s IIS.

.htaccess and sessions for security?

In my application users have their own "websites" which can be reached if they are signed in.
However, since these websites are just directories containing html and other documents everyone in the world can reach them if they know the address. I can't have that :) A user should be able to decide whether or not thw world might see their files or not.
Can I use .htaccess to activate a PHP-script every time a request is made to that directory?
I.e. if reqested-site is "/websites/{identifier}", run is-user-allowed-to-view.php?website={identifier}
The identifier is a numeric value which refers to both a physical folder and a post in the database... and the script would then return true or false.
Or is there perhaps another way of solving the same issue?
Cheers!
You can use mod_rewrite to rewrite requests with such a URL internally to your script:
RewriteEngine on
RewriteRule ^website/([0-9]+)$ is-user-allowed-to-view.php?website=$1
But this rule is only for the URL path /website/12345 and nothing else.
Or have every page as a PHP page and just put at the top a single line to redirect if the session / cookie is incorrect or not set. Obviously wouldn't work for non-PHP content such as images.
What you need is a proper front-end (written in whatever language). You need to have your web-server (Apache in your case it seems) pass the requests to the said front-end.
You cannot do what you are asking for with just .htaccess files.

Resources