301 URL Redirection with value - apache

I need help regarding URL redirection
I want to set a affiliate url like localhost/mysite/bussname/proname, when a viewer will see this link and click it it will redirect to
localhost/mysite/mypage.php?bussinessname=bussname&productname=proname
I already use this code
Redirect 301 localhost/mysite/bussname/pagname localhost/mysite/mypage.php?bussinessname=bussname&productname=proname
It causes internal server URL.
So, what will be the possible code in htaccess. Please help me, thanks in previous.

Try something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^mysite/([^/]+)/([^/]+)$ /mysite/mypage.php?bussinessname=$1&productname=$2 [L,QSA]
You'll need to make sure you have AllowOverride Fileinfo or AllowOverride All in the server config for your document root, and that mod_rewrite is loaded. Then the rules need to go in an htaccess file in your document root.
If you have relative links in your page's content, you may need to change them to absolute URLs or add this to the page's header:
<base href="/mysite/" />

if you want the users to see the new url, you should use redirecting:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^mysite/([^/]+)/([^/]+)$ /mysite/mypage.php?bussinessname=$1&productname=$2 [R=301,L]
otherwise, if you do not want them to see the new url, you should use rewriting:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^mysite/([^/]+)/([^/]+)$ /mysite/mypage.php?bussinessname=$1&productname=$2 [L]
Both codes should be placed inside the .htaccess file in the root directory of your webhost, which should start with the line RewriteEngine ON

Related

Started implementing .htaccess but when navigating from the reduced URL, when going into a directory it then can't get back

So I have included an htaccess file to my server in the root directory and changed all of the ./ I could find set the absolutes.
However, when I search by URL into one of the directories pressing the home button does not take me home. Instead, it appends the index onto the end:
/website/book/index.php?p=home
Instead of
/website/index.php?p=home
Where have I made a fuddle?
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])/$ https://%{HTTP_HOST}/paperbound/$1 [R=301,L]
RewriteRule ^([^/\.]+)?$ index.php?p=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)$ index.php?p=$1&id=$2 [NC,L]
</IfModule>
Is the htaccess used. https://sitehost/website/book/2 is URL entered and page retrieved which exists as https://sitehost/website/index.php?p=book&id=2, clicking navlink to return to https://sitehost/website/index.php?p=home, instead places https://sitehost/website/book/index.php?p=home into the URL bar and returns an error as the file does not exist.
With your shown samples/attempts, please try following htaccess rules file. Make sure to keep your index.php file is present in website folder and htaccess is present along side with website folder(not inside it).
Please make sure to clear your browser cache before testing your URLs.
##Enabling rewrite engine here.
RewriteEngine ON
##Checking conditions for non-existing pages here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##performing internal rewrite here to index.php file.
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ $1/index.php?p=$2&id=$3 [QSA,L]

SEO friendly URL not working

I have a URL (Example.com/blog/) and blog is a directory in my website root. blog contains an index.php file that generates a page based on the query string of ?url=[some url] for example let's say example.com/blog/?url='hello-world' is a valid query string where information can be generated. I want that URL to be example.com/blog/hello-world
Currently, I have a global .htaccess file that removes all extensions. Inside my blog directory is another .htaccess file. It has my attempt at making the URL change to the friendly URL:
RewriteEngine on
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(blog)/(.*)$ blog/index.php?url=$2
Unfortunately, this isn't working. How can I solve this problem so the friendly URL displays?
Try this it like this,
RewriteEngine on
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /blog/?url=$1 [QSA,NC,L]

htaccess and correct url

My problem is that I can't figure out why my htaccess doesn't work.
When I check FTP path it is /var/www/html/folder/subfolder/
Since I am running WP site and I need that subfolder to be accessible, so I have this:
RewriteEngine On
RewriteBase /folder/subfolder/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php
I tried with adding html and of course removing folder but nothing is working.
Can anybody help me?
Thank you.
If you have httpd 2.2.16 or above, you should use FallbackResource instead of all those rewrite rules:
Put a .htaccess inside the folder you wish to have redirects for
Add FallbackResource /index.php
Have cake.

.htaccess two query string cannot link to original folder

Here is the file structure of my web
css/
script/
images/
.htaccess
index.php
ajaxusername.php
.htaccess content
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(\w+)/(\w+)$ index.php?d=$1&t=$2 [QSA]
RewriteRule ^(\w+)/?$ index.php?d=$1
When input http://www.abc.com/peter/, it can translate into http://www.abc.com/index.php?d=peter successfully.
When i input http://www.abc.com/peter/password, it can translate into http://www.abc.com/index.php?d=peter&t=password successfully too.
The problem is:
/peter/ can get the resources listed in above folders successfully but,
/peter/password cannot get those resources.
When I checked in firebug, it shows me it points to http://www.abc.com/peter/css/user.css, which is an invalid URL.
The correct URL must point to http://www.abc.com/css/user.css instead the previous one.
Any suggestion? Thanks!!
Problem is that you're using relative paths for css, js and images and once you start using pretty URL scheme of /part1/part2 it messes up URLs of your resources. As you've noted that css URI becomes /peter/css/user.css instead of /css/user.css.
Here are some solutions for you to help.
Solution 1: To overcome your issue I recommend using absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
Solution 2: It is no doubt better to use absolute path for static files (css, js, images etc). But if you have lots of those instances in several pages then consider using HTML base tag to specify a default URL for relative paths. eg:
<base href="http://www.abc.com/" />
Solution 3: mod_rewrite based solution. Use it when you can't implement above solutions.
Have you .htaccess like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# don't do anything for a real directory, file or link
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule - [L]
# your original rewrite rules
RewriteRule ^(\w+)/?$ /index.php?d=$1 [QSA,L]
RewriteRule ^(\w+)/(\w+)/?$ /index.php?d=$1&t=$2 [QSA,L]
# rules to fix wrong paths of css, js and images
RewriteRule ^[^/]+/((?:css|script|images)/.*)$ /$1 [L,R=301,NC]
It looks like you need to add exceptions to your rewrite rules for requests to files and directories that exist on the server, otherwise when the files are requests by the client's browser they won't be served since the url is rewritten.
Try adding these lines:
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? - [S=2]
into your htaccess file:
Options +FollowSymlinks
RewriteEngine on
...put them here...
RewriteRule ^(\w+)/(\w+)$ index.php?d=$1&t=$2 [QSA]
RewriteRule ^(\w+)/?$ index.php?d=$1
And your resources should be accessible.

Redirect sitename.com/en to sitename.com/ar using htaccess file

I want to redirect sitename.com/en and all links inside that sections to the same urls with /ar instead of /en using htaccess file. The number of links inside the section is enormous, so I can't write them one by one. Please help...
Use this .htaccess :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^en/(.*)?$ /ar/$1 [QSA,L]
If you want that old pages (en/) to no longer be used, and see the new url (ar/) in your browser, use [QSA,L,R=301] instead of [QSA,L] (it notify you're using a permanent redirection)