Re-write links only not redirect - html

Hi I don't know much about htaccess and can't understand how the codes work.
Can someone help me do this
www.site.com/forums/index.php?/cp/3-welcome/
will only change the text itself to
www.site.com/forums/welcome
Not redirect it.

You will first want to ensure rewrite rules are enabled. You can do this through your .htaccess file using code similar to:
<Directory /var/www/website/html>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/images/
RewriteRule ... ...
RewriteRule
</Directory>
Once that is done then you should be able to run your RewriteCond command so that it will successfully redirect as the rewrite engine will be enabled.
Your redirect within the .htaccess file should look something like:
RewriteRule ^http://([^/]*)/forums/welcome(\d+)/(.*)$
http://$1/index.php?/cp/3-welcome/
Of course you may need to update/edit this as required for your specific needs, but this should at least give you a general idea.

Related

.htaccess removing .html won't work with one page

Before i had my site written as .php and i could easily remove .php on end and connect to pages without typing .php on end, but now i got new page that is written as .html and i want to do same. It took me some time to find working code for .htaccess but sadly there is one problem. All pages work fine but when i try to access my help page i get "Error 301 - Access forbidden" for some reason.
For example i type:
example.com/help (original example.com/help.html)
but what it does it turn to:
example.com/help/ and give me that 301 error.
This is my .htaccess:
Options -Indexes
ErrorDocument 404 http://example.com/404.html
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{httpS} off
RewriteRule (.*) https://%{http_HOST}%{REQUEST_URI}
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
So i'm not sure how to fix this (if it's possible), so if anyone has any ideas on how to fix it please let me know. If it's not possible for some reason are there any other pages that this won't work on? I'm still working on site so i can rename help page to helpcenter or something like that but i'm not sure if that effect any other files.
Thanks for help!
Apache2 has supported extension masking via the +MultiViews option for a while now. In my experience, it's much easier to implement than custom rewrite rules.
If you're set on using rewrites, however, make sure you don't have a help directory. -Indexes will give it a 401 if there's no index.html file inside.

Errors with htaccess

Today I tried to write a .htaccess file for first time in order to remove .html extension from url bar along with some other things that I wanted to do. Since I was unfamiliar with all this I read several articles before coding.
I ended up with the following code. I also removed .html from all links. The problem is that when visiting my domain I get the following error.
//The resource you are looking for has been removed,
//had its name changed, or is temporarily unavailable.
Is my code correct?
UPDATED .htaccess
//Rewrite to www
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
//Remove .html UNTIL NOW THIS IS THE ONLY PART
//OF THE CODE THAT ACTUALLY WORKS.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
//Caching schema
<FilesMatch "\.(jpg|png)$">
Header set Cache-Control "private, max-age=160704000"
</FilesMatch>
//Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>
In a separate .htaccess
//Prevent directory listings
Options All -Indexes
MAIN ERRORS
If I click on link like:
Home
Then I get the same error.
Thank you all in advance.
This is native in Apache without using mod_rewrite by using MultiViews option.
Documentation states:
The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.
I finally managed to resolve a part of the problem. For starters I changed my server from windows to linux.
Then to remove html extension I used this code in .htaccess (the other codes didn't work well, giving me an error message)
//Remove .html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
I also removed any .html instances from all links.
Now it works like a charm.

How to htaccess 301 redirect pages with a question mark in the url

I'm trying to redirect several pages that all have question marks in the URL.
I essentially want to redirect:
www.example.com/?attachment_id=456 to www.example.com
There's a ton of pages with differend id #s also.
I've tried a few things in htaccess with no luck..
Any ideas?
This is what I tried:
RewriteCond %{QUERY_STRING} ^attachment_id=[0-9]
RewriteRule ^/$ http://www.example.com/? [L,NC,R=301]
Why can't you do this? This code should redirect a URL like this www.example.com/?attachment_id=456
RewriteCond %{QUERY_STRING} ^attachment_id=[0-9]+
RewriteRule ^/?$ http://www.example.com/? [L,NC,R=301]
I made the / optional so that it can be used in Apache config or .htaccess. Also I kept the ? that you have in the redirect at the end of the RewriteRule to remove any query strings on redirect.
Your approach is next to perfect, just some minor corrections:
RewriteEngine on
RewriteCond %{QUERY_STRING} attachment_id=[0-9]+
RewriteRule ^/$ http://www.example.com/ [L,R=301]
The above is the version for the host configuration. note that you have to restart the http server after having made changes to the host configuration for them to get effective. To debug refer to the http servers error log file, especially at restart time.
If you have to rely on .htaccess style files, then the syntax for the rule itself must unfortunately be slightly different:
RewriteEngine on
RewriteCond %{QUERY_STRING} attachment_id=[0-9]+
RewriteRule ^$ http://www.example.com/ [L,R=301]
Such file has to be located in the main folder of the document root of the host. also the interpretation of such files must be enabled in the host configuration by means of the AllowOverride option.
In general you should always prefer the host configuration for such rules over .htaccess style files, but you need administrative access for that. .htaccess style files are notoriously error prone, hard to debug and really slow the server down.

How to make URLs in a website appear solely as folders

I am trying to design a folder structure for a website project I am working on. A lot of sites these days seem to have the following link structure:
www.example.com/news/news-item-one/
www.example.com/about-us/
Can I make my site work like this without making a new folder for each page I have and putting an index.php file in it?
i.e www.example.com/news/new-item-one.php reads www.example.com/news/news-item-one/
You can use a web application framework like CodeIgniter or CakePHP to do URI routing for you:
http://codeigniter.com/
http://cakephp.org/
This is done with an .htaccess file which either of those frameworks can provide in example documents and they have extensive documentation about URI routing. For example on CI:
http://codeigniter.com/user_guide/general/urls.html
Yes, but it depends what server you are running.
If Apache, one of the most common ways is to create an .htaccess file and use rewrite rules to declare the different routes your website uses.
Below is a very simple example, although not necessarily the best way. There are things you can do to make it more flexible, but I believe it's out of scope of this question. For what it's worth, I prefer a catch-all route that passes route handling to my framework.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^news/(.*)$ news.php?item=$1 [NC,L,QSA]
RewriteRule ^about-us/$ about-us.php [NC,L,QSA]
</IfModule>
You want to use htaccess.
Create a file in your root directory called .htaccess with the following
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ $1.php [NC]
You don't need to use a framework (although it may be a good idea). You can simply setup your .htacess correctly:
http://www.evolt.org/Making_clean_URLs_with_Apache_and_PHP
The following mod_rewrite code (to put in your Apache configuration) will allow you to hide the .php extension of any page you have on your site.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)$ $1.php [L]
It checks to make sure something isn't a file or directory itself, then that adding .php after it's name actually is a file, and it serves that instead.
So if you have a /page.php on your site, going to /page will be the same as going to /page.php.

How can I rewrite all urls to "/"?

Server: Apache
I'm looking to rewrite my urls in the following way, and I can't figure out a way to get it to work.
http://website.com/index.html
I want it to redirect to:
http://website.com/
So basically I want to load index.html, but not display it in the url. I also don't want to give it another name. I'm not looking for /index.html to become /index
Additionally, I'm trying to have any GET data such as:
http://website.com/index.html?id=0&name="fred"
To rewrite to:
http://website.com/?id=0&name="fred".
If you have a solution that is not using .htaccess files that's fine as well.
I believe this should work:
RewriteEngine on
RewriteRule ^index\.html(.*)$ /$1 [R=permanent,L]
This will redirect any requests that start with /index.html to simply "/" and will preserve any arguments that come after index.html
For a .htaccess file:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /index\.html
RewriteRule ^index\.html$ / [L,R=301]