Htaccess redirect rewrite rule custom url - .htaccess

How would I do this please? I am having problems with my htaccess.
suppose I have this url:
www.testingstackzz.com/index.php?action=list&lcp=19
and I want to redirect it to a custom url which I made up like this
www.testingstackzz.com/general/place
where 'place' in the above url is the name for the variable lcp=19 in the database. So lcp=20 would be another place to be more explicit.

In your index.php script, look for the $_GET['lcp'] parameter, look it up in the database, then use header("Location: /general/$name_of_place");
If you only want to limit this to request that directly access the index.php file, then check the $_SERVER['REQUEST_URI'].

Related

htaccess help needed on redirect in php with "unchanged" url

I have a site where I redirect the user to a url, created within a SQL query based on what he entered. So when someone for instance enters http://www.example.com/Something, they are automatically redirected to something like http://www.example.com/Templates/12ES.html?ID=a_very_long_string&URI=Something.
This is all done in one PHP file, and the ID and URI parameters are dynamic.
Is there a way to keep the URL the user entered in the browser while I let PHP do the redirecting silently?
Thanks.
Assuming the .htaccess file in your project root and you're only expecting a single URL segment you could try something along these lines:
RewriteRule ^(.*) /Templates/12ES.html?ID=a_very_long_string&URI=$1 [L]
So long as you don't set the R flag the address in the browser's address bar wont change.

htaccess URL redirect based on page title

I would like to add a line to my htaccess to change this url:
RewriteEngine on
do this is the url
http://site/Calendar/viewevent?eventid=9223
into something like this :
http://site/Calendar/viewevent/Title-of-event
its php and joomla and I am a php developer, please dont advise me to use a component or module to handle redirects, I am trying to achieve this using .htaccess ONLY :) thank you in advance!!
Your 'pretty' url contains information that is not in the 'working' url. Besides that, the 'working' url also contains information that is not in the pretty url. You need database access to translate the event id to a seo-title, and the seo-title back to an event id. In other words: It is impossible to do this with .htaccess only, unless you change 'viewevent' to accept an event by seo-title, instead of eventid.
Mod_dbd can possible be used, but only in the server config file, not .htaccess.
As Sumurai8 says htaccess cant transform url like you want automatically.
But you can use Redirect 301 (or its variation) for one url to another.
for your example:
Redirect 301 /Calendar/viewevent?eventid=9223 /Calendar/viewevent/Title-of-event
more information here

What to do with dynamic url after rewrite?

After you've been able to successfully create a url rewrite how do you handle the original and other possibly ways to access a page. This of course to prevent duplicate content. For example if I have this:
RewriteEngine on
RewriteBase /
RewriteRule ^blog/(\d+)/([\w\-/\.]+)/?$ blog.php?id=$1&article_title=$2 [L]
I'm able to access the page by the url
https://www.mysite.com/blog/10/mysite.com (the mysite.com is the article title)
The problem is I'm also able to access the site by going to
https://www.mysite.com/blog.php?id=10article_title=sitetitle
https://www.mysite.com/blog.php?id=10
ect.
How are you supposed to handle those particular urls.
Also should I change the blog.php?id=10 to the rewritten url? Can I rely on something else and just start using the full rewritten url now? The site is new.
For my web site, I have the script that gets called from inside the rewrite detect the URI they were fetched from (using the "REQUEST_URI" variable that at least Apache sets), and redirect to the canonical one if they ever get called with the internal one (outputting a 301 direction).

Rewrite all requests from a single directory to a script - .htaccess

I need change files url in one folder in my server. It should prevent from direct download but I don't need deny from all rule.
Example:
typical direct link:
http://www.domain.com/foder1/folder2/file.ext
and I need this:
http://www.domain.com/page.html?file=foder1/folder2/file.ext
so in my case I need add this string (page.html?file=) to url. Maybe I'm wrong But I think it must be redirect becouse rewrite not execute page.html just only change (view of) url.
To do this, you need to use mode_rewrite. It will execute the page.html file just fine. It just won't update the URL in the browser bar. So, the user will have no idea they have been directed from the actual file to a script/html page. (As they probably should not!)
Here is an example:
RewriteEngine On
RewriteRule !^foder1/folder2(/|$) page.html?file=%{REQUEST_URI} [L]

Rewrite rules to hide an url

i need to hide full path and show shortly:
www.site.com/this/is/path/to/hide/page.php --> www.site.com
Any idea to do it with .htaccess apache and rewrite rules??
EXAMPLE:
If i type www.site.com i want open index.php (in /),
but if i go to /hidden/path i want to open file.php (in hidden/path)
mantaining browser url in www.site.com.
EDIT:
i want to see in the browser bar www.site.com and i want to open page at /this/is/path/to/hide/page.php .
thanks
As I explained in : How does url rewrite works? every rewrite triggers a new call to the rewritten URL. (HTTP 3xx code).
So the client will ask for www.site.com/this/is/path/to/hide/page.php, would be redirected to www.site.com and will be served the index page as a normal user.
There is no way to tell the client to display one URL in the browser bar instead of another, client browser will always make a new request. (Or you could impersonate any site for example)
If you are not against the use of a cookie, or can use environment variable you may be able to do something like :
RewriteRule this/is/path/to/hide/page.php / [co:knowHiddenPath=true]
The environment variable as same syntax with E instead of co.
(See http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html for cookie information)
Your index page should then check this cookie/variable to serve the hidden page or not.
Another solution would be to enable access with password to your file. So even if someone know the URL he would not access the file. Security by obscurity is does not exists.
You can I believe do that with an Alias,
Alias / /this/is/path/to/hide/page.php
This directive needs to be in your <VirtualHost>
This will use mod_rewrite and you can put this into your .htaccess
# Activate Rewrite Engine
RewriteEngine On
# Home page rewrite rule
RewriteRule ^$ /this/is/path/to/hide/page.php [QSA,L]
This will ONLY work if you hit website root (e.g. http://www.example.com/)

Resources